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. Donot use it in a production deployment. Use a production WSGI serverinstead. * 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 方法:
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 isa 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 toquit) * Restarting with stat * Debugger is active! * Debugger PIN: 461-774-216 post {'hi': 'world', 'way': 'post'} 127.0.0.1 - - [21/Jun/202011:42:48] "POST /add/post HTTP/1.1"200 - $ python test_post.py # 测试文件 {'hi': 'world', 'way': 'post'}