python web 框架例子_最快的 Python Web 框架入门
原标题:最快的 Python Web 框架入门
来源:Python开发
ID:PythonPush
速度比较
框架
实现基础
每秒请求数
平均时间
Sanic
Python 3.5 + uvloop
30,601
3.23ms
Wheezy
gunicorn + meinheld
20,244
4.94ms
Falcon
gunicorn + meinheld
18,972
5.27ms
Bottle
gunicorn + meinheld
13,596
7.36ms
Flask
gunicorn + meinheld
4,988
20.08ms
Kyoukai
Python 3.5 + uvloop
3,889
27.44ms
Aiohttp
Python 3.5 + uvloop
2,979
33.42ms安装
环境:python3.5+
python -m pip install sanic
Hello World
创建文件main.py,写入下面的内容
from sanic import Sanic
from sanic.response import json
app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({ "hello": "world" })
app.run(host="0.0.0.0", port=8000)
运行 python3 main.py
sanic是不是看起来和flask一样
Request
属性
request.files (dictionary of File objects) - 上传文件列表
request.json (any) - json数据
request.args (dict) - get数据
request.form (dict) - post表单数据
例子
from sanic import Sanic
from sanic.response import json
@app.route("/json")
def post_json(request):
return json({ "received": True, "message": request.json })
@app.route("/form")
def post_json(request):
return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
@app.route("/files")
def post_json(request):
test_file = request.files.get('test')
file_parameters = {
'body': test_file.body,
'name': test_file.name,
'type': test_file.type,
}
return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
@app.route("/query_string")
def query_string(request):
return json({ "parsed": True, "args": request.args, "url": request.url, "query_string": request.query_string })路由
和flask差不多,一看就懂
from sanic import Sanic
from sanic.response import text
@app.route('/tag/')
async def person_handler(request, tag):
return text('Tag - {}'.format(tag))
@app.route('/number/')
async def person_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg))
@app.route('/number/')
async def person_handler(request, number_arg):
return text('Number - {}'.format(number))
@app.route('/person/')
async def person_handler(request, name):
return text('Person - {}'.format(name))
@app.route('/folder/')
async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id))注册中间件
app = Sanic(__name__)
@app.middleware
async def halt_request(request):
print("I am a spy")
@app.middleware('request')
async def halt_request(request):
return text('I halted the request')
@app.middleware('response')
async def halt_response(request, response):
return text('I halted the response')
@app.route('/')
async def handler(request):
return text('I would like to speak now please')
app.run(host="0.0.0.0", port=8000)异常处理
抛出异常
from sanic import Sanic
from sanic.exceptions import ServerError
@app.route('/killme')
def i_am_ready_to_die(request):
raise ServerError("Something bad happened")
处理异常
from sanic import Sanic
from sanic.response import text
from sanic.exceptions import NotFound
@app.exception(NotFound)
def ignore_404s(request, exception):
return text("Yep, I totally found the page: {}".format(request.url))蓝图
和flask中的蓝图一样,用于组织项目结构
创建一个蓝图,相当于创建一个sanic app,上面的用法和上面相同,把app改成蓝图名称bp
from sanic.response import json
from sanic import Blueprint
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root():
return json({'my': 'blueprint'})
蓝图注册到主app
from sanic import Sanic
from my_blueprint import bp
app = Sanic(__name__)
app.register_blueprint(bp)
app.run(host='0.0.0.0', port=8000, debug=True)总结
sanic将是一个非常流行的框架.因为它基于python3.5+,使用了许多新的特性,这些特性让程序速度更快。返回搜狐,查看更多
责任编辑:
python web 框架例子_最快的 Python Web 框架入门相关推荐
- python视频换脸例子_超简单使用Python换脸实例
换脸! 这段时间,deepfakes搞得火热,比方说把<射雕英雄传>里的朱茵换成了杨幂,看下面的图!毫无违和感! 其实早在之前,基于AI换脸的技术就得到了应用,比方说<速度与激情7& ...
- python 前端框架比较_浅谈五大Python Web框架
说到Web Framework,Ruby的世界Rails一统江湖,而Python则是一个百花齐放的世界,各种micro-framework.framework不可胜数,不完全列表见: 虽然另一大脚本语 ...
- python语言运行效率高_如何评价说PYTHON是最快的语言?
我是视频的作者,首先感谢朋友们的关注,并且诞生了这个论题,让我可以看到大家对我视频中的观点的反馈.对于大家的讨论,在日常时间允许的条件下,我会尽量参与.下面就我目前已经看到的朋友们的评论,做一下展开. ...
- python语句的基本框架开会_跟我学Python从小白到入门 之 Part 4 操作列表002
Python从小白到入门 ~ Part 4 操作列表 002 手绘题图~奈良若草山的夏天Python是一门杰出的语言,值得你去学习,咱们现在就开始吧.--<Python编程从入门到实践> ...
- java 框架 例子_如何设计Java框架? –一个简单的例子
通过优锐课核心java学习笔记中,我们可以看到,码了很多专业的相关知识, 分享给大家参考学习. 你可能对框架如何工作感到好奇? 这里将通过一个简单的框架示例来说明框架的思想. 框架目标 首先,为什么我 ...
- python描述器 有限状态机_笨办法学 Python · 续 练习 30:有限状态机
练习 30:有限状态机 每当你阅读一本关于解析的书,都有一个可怕的章节,关于有限状态机(FSM).他们对"边"和"节点"进行了详细的分析,每个可能的" ...
- 小学生python编程写游戏_小学生开始学Python,开发AI的首选编程语言:推荐一波Python书单...
AlphaGo 都在使用的 Python 语言,是最接近 AI 的编程语言. 教育部考试中心近日发布了"关于全国计算机等级(NCRE)体系调整"的通知,决定自2018年3月起,在全 ...
- python上网行为分析_转:用 Python 一键分析你的上网行为, 看是在认真工作还是摸鱼...
简介 想看看你最近一年都在干嘛?看看你平时上网是在摸鱼还是认真工作?想写年度汇报总结,但是苦于没有数据?现在,它来了. 这是一个能让你了解自己的浏览历史的Chrome浏览历史记录分析程序,当然了,他仅 ...
- python数据处理电脑配置_『大数据python计算机基础教学视频教程』入门学python需要什么配置的电脑...
x = 1 if x 4.2 1) else匹前的缩进相同且最接近的if 2) 改之前y=x 2行,之后是y=x 1先执行 第5题 代码还n = 1 while n <= 5: i = 1 wh ...
最新文章
- MATLAB的图像文件怎么标字母,用matlab对图片进行字符识别,只要能识别字母就行…十万火急!!请各位大侠们多多帮忙…...
- Python3.7 中Scipy和Numpy的安装(含下载资源)
- java实现简单链表
- JS中对于email格式的判断
- springboot(三):Spring boot中Redis的使用
- mysql 读写分离 ,mysql_proxy实现
- 转:Flash与.NET的通信(一):XMLConnector的应用
- 第八届蓝桥杯-日期问题
- 06_Influxdb+Grafana绘图基础
- ubuntu没有进入图形界面解决办法
- C#字典类型转URL参数字符串
- 一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 (转)
- 模式识别算法:SVM支持向量机
- 文泉驿正黑 font JAVA,字体 | 免费可商用字体~文泉驿正黑体
- 德标螺纹规格对照表_英制螺纹对照表详细介绍,英制螺丝螺纹标准
- Android 关于微信分享提示签名不对的问题
- harry -考级复习2
- gitee代码管理仓库管理代码,更简单地查看各阶段的代码变动
- 长时间 正在加载个人设置 开机很慢
- Deep Blind Video Super-resolution