使用flask-restful搭建API
最简单的例子 访问 http://127.0.0.1:5000/ , 返回 {"hello": "world"} from flask import Flask from flask_restful import Resource, Api app = Flask(__name__) api = Api(app) class HelloWorld(Resource): #继承自flask_restful.Resource类 def get(self): #定义来自请求的方法, 例如get,post return {'hello': 'world'} api.add_resource(HelloWorld, '/') if __name__ == '__main__': app.run(debug=True) 通过路由访问 命令行执行 curl http://localhost:5000/todo1 -d "data=Remember the milk" -X PUT , 返回 {"todo1": "Remember the milk"} . 执行 curl http://localhost:5000/todo1 ,返回 {"todo1": "Remember the milk"} from flask import Flask, request from flask_restful