flask开发restful风格api
先从flask最简单的例子开始,hello world~
1 #!venv/bin/python 2 from flask import Flask 3 app = Flask(__name__) 4 5 @app.route('/') 6 def index(): 7 return 'hello world\n' 8 9 if __name__ == '__main__': 10 print 'server start...' 11 app.run()
来测试一下这个server吧
1 ▶ curl 127.0.0.1:5000 2 hello world
再看一下server端
▶ ./app.py server start... * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 - - [19/Mar/2018 13:05:24] "GET / HTTP/1.1" 200 -
再来看一下如何添加其他的http方法
#!venv/bin/python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api', methods = ['GET']) def api_get(): return jsonify({'msg':'this is a GET method'}) @app.route('/api', methods = ['POST']) def api_post(): return jsonify({'msg':'this is a POST method'}) @app.route('/api', methods = ['PUT']) def api_put(): return jsonify({'msg':'this is a PUT method'}) @app.route('/api', methods = ['DELETE']) def api_del(): return jsonify({'msg':'this is a DELETE method'}) if __name__ == '__main__': print 'server start...' app.run()
以PUT方法为例,看一下结果
▶ curl -X PUT 127.0.0.1:5000/api { "msg": "this is a PUT method" }
接下来就是设计数据模型。

浙公网安备 33010602011771号