1、源代码

 

#!/usr/bin/python
#encoding=utf-8

from flask import json, Flask, request

app = Flask(__name__)

@app.route('/project/', methods=['GET', 'POST'] )
def hello():
  if request.method == 'GET':
    return '[GET METHOD] your name is ' + request.args.get('name', 'unknown')
  else:
    return '[POST METHOD] your name is '+ request.form.get('name', 'unknown')


if __name__ == '__main__':
  app.run(port=7777, debug=True, host='127.0.0.1')

  

2、运行代码

在pycharm 中运行debug 模式。

 

3、测试代码

测试GET操作:

[2019-10-07 20:03.03] /drives/d/code/my-python/flask
[zhou.he_zhou] ➤ curl "http://127.0.0.1:7777/project/?name=bb"
[GET METHOD] your name is bb ✔
─────────────────────────────

测试POST操作:

[2019-10-07 20:04.03] /drives/d/code/my-python/flask
[zhou.he_zhou] ➤ curl -d "name=dd" "http://127.0.0.1:7777/project/"
[POST METHOD] your name is dd

posted on 2019-10-07 20:34  爱编程的小兵  阅读(1922)  评论(0编辑  收藏  举报