最快的 Python Web 框架入门
速度比较
框架 | 实现基础 | 每秒请求数 | 平均时间 |
---|---|---|---|
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)
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+,使用了许多新的特性,这些特性让程序速度更快。
看完以上的内容,相信你对于Linux运维的了解又加深了一层。作为一名Linux爱好者,如果你在学习中遇到了困惑需要交流,可以来我们的网站(http://www.magedu.com/)获取帮助,了解行业评价最高的Linux课程可以拨打电话:18519746220。
最快的 Python Web 框架入门的更多相关文章
- Python Flask Web 框架入门
Python Flask 目录 本文主要借鉴 letiantian 的文章 http://www.letiantian.me/learn-flask/ 一.简介 二.安装 三.初始化Flask 四.获 ...
- 一步一步理解 python web 框架,才不会从入门到放弃
要想清楚地理解 python web 框架,首先要清楚浏览器访问服务器的过程. 用户通过浏览器浏览网站的过程: 用户浏览器(socket客户端) 3. 客户端往服务端发消息 6. 客户端接收消息 7. ...
- python三大web框架Django,Flask,Flask,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Python几种主流框架 从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python We ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
- 教程:Visual Studio 中的 Django Web 框架入门
教程:Visual Studio 中的 Django Web 框架入门 Django 是高级 Python 框架,用于快速.安全及可扩展的 Web 开发. 本教程将在 Visual Studio 提供 ...
- 浅谈Python web框架
一.Python web框架 Web Framework,Ruby的世界Rails一统江湖,而Python则是一个百花齐放的世界,各种micro-framework.framework不可胜数,不完全 ...
- python web框架介绍对比
Django Python框架虽然说是百花齐放,但仍然有那么一家是最大的,它就是Django.要说Django是Python框架里最好的,有人同意也有人 坚决反对,但说Django的文档最完善.市场占 ...
- “脚踢各大Python Web框架”,Sanic真有这能耐么?
在Github上,Sanic第一句介绍语就是: "Sanic is a Flask-like Python 3.5+ web server that's written to go fast ...
- Python web框架——Tornado
Tornado是一个Python Web框架和异步网络库,最初由FriendFeed开发.通过使用非阻塞网络I / O,Tornado可以扩展到数万个开放连接,使其成为需要长时间连接每个用户的长轮询, ...
随机推荐
- HDU 1248寒冰王座-全然背包或记忆化搜索
寒冰王座 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 改动wordpress默认发邮件邮箱地址
方法一: wordpress发送邮件默认是用的php mail函数.所以发送邮件的地址是能够随意写的.其默认发邮件的地址是wordpress@yourdomain.com.当然这个地址是能够改动的. ...
- Android BroadcastAnyWhere(Google Bug 17356824)漏洞具体分析
Android BroadcastAnyWhere(Google Bug 17356824)漏洞具体分析 作者:简行(又名 低端码农) 继上次Android的LaunchAnyWhere组件安全漏洞后 ...
- Linux下Java线程具体监控和其dump的分析使用----分析Java性能瓶颈[张振华-Jack]
作者:张振华(Jack) 这里对linux下.sun(oracle) JDK的线程资源占用问题的查找步骤做一个小结: linux环境下,当发现java进程占用CPU资源非常高,且又要想更进一步查出哪一 ...
- unity3D游戏开发实战原创视频讲座系列7之消消乐游戏开发
解说文件夹 第一讲 游戏介绍和资源简单介绍 第二讲 游戏场景背景的搭建 第三讲 游戏特效预制体的制作 第四讲 游戏场景前景的显示 第五讲 瓷砖背景块 第六讲 方块的消除 第七讲 方块的交 ...
- 将分布式-队列的实现交给redis
import requestsimport reimport timefrom redis import Redisimport threading REDIS_HOST, REDIS_PORT, P ...
- remove namespace from xml config file
从xml配置文件中移除命令空间 https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-wit ...
- CF732 F Tourist Reform——边双连通分量
题目:http://codeforces.com/contest/732/problem/F 首先把边双缩点,边双内部 dfs 一个顺序一定是可以从每个点走到边双内部所有点的,因为它是以环为基本单位: ...
- openstack 杂记 备忘
- PCB MS SQL 标量函数与表值函数(CLR) 实现文件与目录操作
一.C#写SQL SERVER(CLR)实现文件操作 标量函数: 文件移动 ,复制,检测文件存在,写入新文件文本,读取文本,创建目录,删除目录,检测目录是否存在 /// <summary> ...