sanic是一款用 python3.5+ 寫的 web framework,用法和 flask 類似,特點是非???nbsp;。
Github 官網(wǎng):https://github.com/channelcat/sanic
速度比較
框架實現(xiàn)基礎(chǔ)每秒請求數(shù)平均時間
SanicPython 3.5 + uvloop30,6013.23ms
Wheezygunicorn + meinheld20,2444.94ms
Falcongunicorn + meinheld18,9725.27ms
Bottlegunicorn + meinheld13,5967.36ms
Flaskgunicorn + meinheld4,98820.08ms
KyoukaiPython 3.5 + uvloop3,88927.44ms
AiohttpPython 3.5 + uvloop2,97933.42ms
安裝
環(huán)境:python3.5+
python -m pip install sanic
Hello World
創(chuàng)建文件main.py,寫入下面的內(nèi)容
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數(shù)據(jù)
request.args (dict) - get數(shù)據(jù)
request.form (dict) - post表單數(shù)據(jù)
例子
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))
藍(lán)圖
和flask中的藍(lán)圖一樣,用于組織項目結(jié)構(gòu)
創(chuàng)建一個藍(lán)圖,相當(dāng)于創(chuàng)建一個sanic app,上面的用法和上面相同,把app改成藍(lán)圖名稱bp
from sanic.response import json
from sanic import Blueprint
bp = Blueprint('my_blueprint')
@bp.route('/')
async def bp_root():
return json({'my': 'blueprint'})
藍(lán)圖注冊到主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)
總結(jié)
sanic將是一個非常流行的框架.因為它基于python3.5+,使用了許多新的特性,這些特性讓程序速度更快。
作者:Prasanta