Python Flask Restful
Flask Restful
1.flask restful
在flask基础上进行一些封装,主要用于实现restful接口
2.restful的理解
1)URI(统一资源标识符):每一个URI代表一种资源,restful接口对资源进行增删改查
2) 客户端和服务器之间,传递这种资源的某种表现层
3)客户端通过四个HTTP动词(get、post、put、delete),对服务器资源进行操作,实现“表现层状态转化”
3.Flask Restful安装
1)先安装flask:
pip install flask
2)再安装flask restful:
pip install flask-restful
4.HelloWorld
__author__ = 'zhizhi'
from flask import Flask
from flask_restful import Api,Resource#老版本flask中使用的是flask.ext中的Api,目前已经被flask_restful中的Api替代
app=Flask(__name__)
api=Api(app) class HelloWorld(Resource):
def get(self):
return {"hello":"world"} api.add_resource(HelloWorld,'/') if __name__=='__main__':
app.run(debug=True)

结果:

5.flask restful API官网示例
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)
6.官网示例的代码解析
(1)引入需要的库名、函数、变量等,并做简单的Application初始化:
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource app = Flask(__name__)
api = Api(app)
(2)定义我们需要操作的资源类型(都是json格式的):
TODOS = {
'todo1': {'task': 'build an API'},
'todo2': {'task': '?????'},
'todo3': {'task': 'profit!'},
}
(3)如果id不存在,则停止运行
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
abort(404, message="Todo {} doesn't exist".format(todo_id))
(4)Flask-RESTful提供了一个用于参数解析的RequestParser类,类似于Python中自带的argparse类,可以很方便的解析请求中的-d参数,并进行类型转换。
parser = reqparse.RequestParser()
parser.add_argument('task')
(5)API分为两类:带参数id的,和不带有参数id的。前者是操作单一资源,后者是操作资源列表或新建一个资源。
从操作单一资源开始,继承Resource类,并添加put / get / delete方法:
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
(6)继续操作资源列表,继承Resource类,并添加get / post方法:
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
(7)资源操作类定义完毕之后,需要设置路由,即告诉Python程序URL的对应关系。
api.add_resource(TodoList, '/todos')
api.add_resource(Todo, '/todos/<todo_id>')
Python Flask Restful的更多相关文章
- python Flask restful框架
框架地址:https://github.com/flask-restful/flask-restful 文档:http://flask-restful.readthedocs.io/en/0.3.5/ ...
- [转]python实现RESTful服务(基于flask)
python实现RESTful服务(基于flask) 原文: https://www.jianshu.com/p/6ac1cab17929 前言 上一篇文章讲到如何用java实现RESTful服务, ...
- Python Flask高级编程之RESTFul API前后端分离精讲 (网盘免费分享)
Python Flask高级编程之RESTFul API前后端分离精讲 (免费分享) 点击链接或搜索QQ号直接加群获取其它资料: 链接:https://pan.baidu.com/s/12eKrJK ...
- Python flask 构建可扩展的restful apl☝☝☝
Python flask 构建可扩展的restful apl☝☝☝ Flask-RESTful是flask的扩展,增加了对快速构建REST API的支持.Flask-RESTful通过最少的设置鼓励最 ...
- Python flask 构建可扩展的restful apl✍✍✍
Python flask 构建可扩展的restful apl 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课 ...
- python实现Restful服务(基于flask)(2)
参考:https://blog.csdn.net/yelena_11/article/details/53404892 最简单的post例子: from flask import Flask, req ...
- python 部署 Restful web
使用python web做Restful 风格,很简单,采用Flask框架轻松实现一个RESTful的服务. Restful相关介绍请查看:https://www.ibm.com/developerw ...
- Flask RESTful API搭建笔记
之前半年时间,来到项目的时候,已经有一些东西,大致就是IIS+MYSQL+PHP. 所以接着做,修修补补,Android/iOS与服务器数据库交换用PHP, Web那边则是JS+PHP,也没有前后端之 ...
- Python Flask Web 框架入门
Python Flask 目录 本文主要借鉴 letiantian 的文章 http://www.letiantian.me/learn-flask/ 一.简介 二.安装 三.初始化Flask 四.获 ...
随机推荐
- Linux 之 rsyslog+mysql+LogAnalyzer 日志收集系统
作者:邓聪聪 LogAnalyzer 是一个 syslog 和其他网络事件数据的 Web 前端工具,提供简单易用的日志浏览.搜索和基本分析以及图表显示 由于公司部分项目需求使用日志记录系统,随笔记录 ...
- having的用法
转载:http://blog.csdn.net/oathevil/article/details/5521757 where和having: “Where” 是一个约束声明,使用Where来约束来自于 ...
- Linux su切换用户后命令提示符变为bash-4.2$
2018-9-30 19:31:41 星期日 今天遇到一个问题, 给gitlab配置webhook的时候, 一个目录总是不能正确执行git pull 命令, 无任何输出, 根据之前经验, 感觉是权限的 ...
- springboot集成mybatis源码分析-启动加载mybatis过程(二)
1.springboot项目最核心的就是自动加载配置,该功能则依赖的是一个注解@SpringBootApplication中的@EnableAutoConfiguration 2.EnableAuto ...
- Python内置模块之subprocess
import subprocess ret = subprocess.Popen('netstat -ano',shell=True,stdout=subprocess.PIPE,stderr=sub ...
- 在ABP的Web层中实现复杂请求跨域访问
在最近的项目中,后端使用ABP,前端采用React,前后端完全分离.其中大部分接口都通过WebApi层调用,项目中未使用Session.但最后在添加一个网站的验证码验证留言功能时,使用了Session ...
- 第四章:4.0 python常用的模块
1.模块.包和相关语法 使用模块好处: 最大的好处是大大提高了代码的可维护性.其次,编写代码不必从零开始.当一个模块编写完毕,就可以被其他地方引用.我们在编写程序的时候,也经常引用其他模块,包括Pyt ...
- 一个div实现白眼效果
巧妙利用border和background-clip <div class="eye"></div> .eye{ width:150px; height ...
- 网络编程-Python高级语法-GIL全局解释器锁
知识点:GIL全局解释器锁其实和Python没有任何关系,是由于当初编写Python解释器时留下的,它只对多线程有影响,GIL保证同一时刻只有一个线程在运行,即使是多核配置电脑,同一时刻也只会让一个线 ...
- LOJ.6160.[美团CodeM初赛 RoundA]二分图染色(容斥 组合)
题目链接 \(Description\) 求在\(2n\)个点的完全二分图(两边各有\(n\)个点)上确定两组匹配,使得两个匹配没有交集的方案数. \(n\leq10^7\). \(Solution\ ...