[Flask]接口实现和测试

小结Flask框架使用,包括Get/Post接口实现和测试

Flask简介

Flask是一个使用Python编写的轻量级Web应用框架

安装

参考:安装

1
2
# pip也行
$ conda install flask

一个最小的应用

新建app.py

1
2
3
4
5
6
7
8
9
10
$ cat app.py 
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

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

启动flask应用

1
2
3
4
5
6
7
8
9
10
$ python app.py 
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 461-774-216

测试

1
2
$ curl http://localhost:5000
Hello, World!

Get/Post接口

Web 应用使用不同的 HTTP 方法处理 URL 。当你使用 Flask 时,应当熟悉 HTTP 方法。 缺省情况下,一个路由只回应 GET 请求。 可以使用 route() 装饰器的 methods 参数来处理不同的 HTTP 方法:

修改app.py,添加Get/Post接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ cat app.py 
from flask import Flask, request
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "it's POST"
else:
return "it's GET"

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

测试

1
2
3
4
$ curl http://localhost:5000/login
it's GET
$ curl -X POST http://localhost:5000/login
it's POST

测试文件

编写测试程序进行请求。修改app.py如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$ cat app.py 
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
app.debug = True

@app.route('/')
def hello_world():
return 'Hello, World!'

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return "it's POST"
else:
return "it's GET"

@app.route('/add/post', methods=['post'])
def add_stu():
if not request.data:
return ('fail')
post_data = request.data.decode('utf-8')
post_json = json.loads(post_data)
print(post_json['way'])
print(post_json)
# 返回json数据
return jsonify(post_json)

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

测试文件如下:

1
2
3
4
5
6
7
8
9
10
11
$ cat test_post.py 
import requests,json

data = {
'hi': 'world',
'way': 'post'
}
url = 'http://localhost:5000/add/post'

r = requests.post(url, data=json.dumps(data))
print(r.json())

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ python app.py                                                  # app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 461-774-216
post
{'hi': 'world', 'way': 'post'}
127.0.0.1 - - [21/Jun/2020 11:42:48] "POST /add/post HTTP/1.1" 200 -
$ python test_post.py # 测试文件
{'hi': 'world', 'way': 'post'}

相关阅读