标签:
Flask-RESTful 是一个 Flask 扩展,它添加了快速构建 REST APIs 的支持。它当然也是一个能够跟你现有的ORM/库协同工作的轻量级的扩展。
api.py
from flask import Flask from flask.ext import restful app = Flask(__name__) api = restful.Api(app) class HelloWorld(restful.Resource): def get(self): return {‘hello‘: ‘world‘} api.add_resource(HelloWorld, ‘/‘) if __name__ == ‘__main__‘: app.run(debug=True)
运行
$ python api.py * Running on http://127.0.0.1:5000/
另打开一个命令窗口,测试这个API:
$ curl http://127.0.0.1:5000/ {"hello": "world"}
心得
证例如下
jihite@ubuntu:~$ curl http://127.0.0.1:5000 { "hello": "world" } jihite@ubuntu:~$ curl http://127.0.0.1:5000 -X GET { "hello": "world" } jihite@ubuntu:~$ curl http://127.0.0.1:5000 -X get { "hello": "world" } jihite@ubuntu:~$ curl http://127.0.0.1:5000 -X Get { "hello": "world" }
Flask-RESTful 提供的最主要的基础就是资源(resources)
api_Routing.py
from flask import Flask, request from flask.ext.restful import Resource, Api app = Flask(__name__) api = Api(app) todos = {} class TodoSimple(Resource): def get(self, todo_id): return {todo_id: todos[todo_id]} def put(self, todo_id): todos[todo_id] = request.form[‘data‘] return {todo_id: todos[todo_id1 api.add_resource(TodoSimple, ‘/<string:todo_id>‘) if __name__ == ‘__main__‘: app.run(debug=True)
运行
$ python api_Routing.py * Running on http://127.0.0.1:5000/
另一命令窗口
jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo1 -d ‘data=apple‘ -X PUT { "todo1": "apple" } jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo1 { "todo1": "apple" } jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo2 -d ‘data=banana‘ -X PUT { "todo2": "banana" } jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo2 { "todo2": "banana" }
心得
证例如下
jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo1 -d ‘data=jimi‘ -X PUT { "todo1": "jimi" } jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo1 -X GET { "todo1": "jimi" } jihite@ubuntu:~$ curl http://127.0.0.1:5000/todo1 -d ‘task=jimi‘ -X PUT { "message": "The browser (or proxy) sent a request that this server could not understand." #server无法理解请求 }
api_mulout.py
from flask import Flask, request from flask.ext.restful import Resource, Api app = Flask(__name__) api = Api(app) todos = {} class Todo1(Resource): def get(self): return {‘task‘: ‘apple‘} class Todo2(Resource): def get(self): return {‘task‘: ‘babana‘}, 201 class Todo3(Resource): def get(self): return {‘task‘: ‘pear‘}, 201, {‘data‘: ‘peach‘} api.add_resource(Todo1, ‘/Todo1‘) api.add_resource(Todo2, ‘/Todo2‘) api.add_resource(Todo3, ‘/Todo3‘) if __name__ == "__main__": app.run(debug=True)
运行
$ python api_mulout.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat
另一命令行窗口
jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/Todo1 { "task": "apple" } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/Todo2 { "task": "babana" } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/Todo3 { "task": "pear" } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/Todo4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
心得
api_points.py
from flask import Flask from flask.ext import restful app = Flask(__name__) api = restful.Api(app) class HelloWorld(restful.Resource): def get(self): return {‘hello‘: ‘world‘} api.add_resource(HelloWorld, ‘/‘, ‘/hello‘) if __name__ == ‘__main__‘: app.run(debug=True)
运行
$ python api_points.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat
另一个命令窗口执行
jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/ { "hello": "world" } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/hello { "hello": "world" }
whole.py
from flask import Flask from flask.ext.restful import reqparse, abort, Api, Resource app = Flask(__name__) api = Api(app) TODOS = { ‘todo1‘: {‘task‘: ‘build an API‘}, ‘todo2‘: {‘task‘: ‘?????‘}, ‘todo3‘: {‘task‘: ‘profit!‘}, } def abort_if_todo_doesnt_exist(todo_id): if todo_id not in TODOS: abort(404, message="Todo {} doesn‘t exist".format(todo_id)) parser = reqparse.RequestParser() parser.add_argument(‘task‘, type=str) # Todo # show a single todo item and lets you delete them class Todo(Resource): def get(self, todo_id): abort_if_todo_doesnt_exist(todo_id) return TODOS[todo_id] def delete(self, todo_id): abort_if_todo_doesnt_exist(todo_id) del TODOS[todo_id] return ‘‘, 204 def put(self, todo_id): args = parser.parse_args() task = {‘task‘: args[‘task‘]} TODOS[todo_id] = task return task, 201 # TodoList # shows a list of all todos, and lets you POST to add new tasks class TodoList(Resource): def get(self): return TODOS def post(self): args = parser.parse_args() todo_id = int(max(TODOS.keys()).lstrip(‘todo‘)) + 1 todo_id = ‘todo%i‘ % todo_id TODOS[todo_id] = {‘task‘: args[‘task‘]} return TODOS[todo_id], 201 ## ## Actually setup the Api resource routing here ## api.add_resource(TodoList, ‘/todos‘) api.add_resource(Todo, ‘/todos/<todo_id>‘) if __name__ == ‘__main__‘: app.run(debug=True)
运行
$ python whole.py * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) * Restarting with stat
另一个命令窗口执行
jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos { "todo1": { "task": "build an API" }, "todo2": { "task": "?????" }, "todo3": { "task": "profit!" } } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos/todo2 { "task": "?????" } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos -d ‘task=hello‘ -X POST -v * Hostname was NOT found in DNS cache * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0) > POST /todos HTTP/1.1 > User-Agent: curl/7.37.1 > Host: 127.0.0.1:5000 > Accept: */* > Content-Length: 10 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 10 out of 10 bytes * HTTP 1.0, assume close after body < HTTP/1.0 201 CREATED < Content-Type: application/json < Content-Length: 24 < Server: Werkzeug/0.10.4 Python/2.7.8 < Date: Tue, 14 Jul 2015 06:29:19 GMT < { "task": "hello" } * Closing connection 0 jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos { "todo1": { "task": "build an API" }, "todo2": { "task": "?????" }, "todo3": { "task": "profit!" }, "todo4": { "task": "hello" } } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos/todo2 -X DELETE -v * Hostname was NOT found in DNS cache * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0) > DELETE /todos/todo2 HTTP/1.1 > User-Agent: curl/7.37.1 > Host: 127.0.0.1:5000 > Accept: */* > * HTTP 1.0, assume close after body < HTTP/1.0 204 NO CONTENT < Content-Type: application/json < Content-Length: 0 < Server: Werkzeug/0.10.4 Python/2.7.8 < Date: Tue, 14 Jul 2015 06:29:37 GMT < * Closing connection 0 jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos { "todo1": { "task": "build an API" }, "todo3": { "task": "profit!" }, "todo4": { "task": "hello" } } jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/todos/todo3 -d ‘task=misi‘ -X PUT -v * Hostname was NOT found in DNS cache * Trying 127.0.0.1... * Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0) > PUT /todos/todo3 HTTP/1.1 > User-Agent: curl/7.37.1 > Host: 127.0.0.1:5000 > Accept: */* > Content-Length: 9 > Content-Type: application/x-www-form-urlencoded > * upload completely sent off: 9 out of 9 bytes * HTTP 1.0, assume close after body < HTTP/1.0 201 CREATED < Content-Type: application/json < Content-Length: 23 < Server: Werkzeug/0.10.4 Python/2.7.8 < Date: Tue, 14 Jul 2015 06:30:24 GMT < { "task": "misi" } * Closing connection 0 jihite@ubuntu:~/project/flask$ curl http://127.0.0.1:5000/4 { "todo1": { "task": "build an API" }, "todo3": { "task": "misi" }, "todo4": { "task": "hello" } }
标签:
原文地址:http://www.cnblogs.com/kaituorensheng/p/4645080.html