日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

最快的 Python Web 框架入門

 達坂城大豆 2017-12-14

sanic是一款用 python3.5+ 寫的 web framework,用法和 flask 類似,特點是非???/strong> 。


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)容

  1. from sanic import Sanic

  2. from sanic.response import json

  3. app = Sanic(__name__)

  4. @app.route('/')

  5. async def test(request):

  6.    return json({ 'hello': 'world' })

  7. 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ù)

例子

  1. from sanic import Sanic

  2. from sanic.response import json

  3. @app.route('/json')

  4. def post_json(request):

  5.    return json({ 'received': True, 'message': request.json })

  6. @app.route('/form')

  7. def post_json(request):

  8.    return json({ 'received': True, 'form_data': request.form, 'test': request.form.get('test') })

  9. @app.route('/files')

  10. def post_json(request):

  11.    test_file = request.files.get('test')

  12.    file_parameters = {

  13.        'body': test_file.body,

  14.        'name': test_file.name,

  15.        'type': test_file.type,

  16.    }

  17.    return json({ 'received': True, 'file_names': request.files.keys(), 'test_file_parameters': file_parameters })

  18. @app.route('/query_string')

  19. def query_string(request):

  20.    return json({ 'parsed': True, 'args': request.args, 'url': request.url, 'query_string': request.query_string })

路由

和flask差不多,一看就懂

  1. from sanic import Sanic

  2. from sanic.response import text

  3. @app.route('/tag/')

  4. async def person_handler(request, tag):

  5.    return text('Tag - {}'.format(tag))

  6. @app.route('/number/')

  7. async def person_handler(request, integer_arg):

  8.    return text('Integer - {}'.format(integer_arg))

  9. @app.route('/number/')

  10. async def person_handler(request, number_arg):

  11.    return text('Number - {}'.format(number))

  12. @app.route('/person/')

  13. async def person_handler(request, name):

  14.    return text('Person - {}'.format(name))

  15. @app.route('/folder/')

  16. async def folder_handler(request, folder_id):

  17.    return text('Folder - {}'.format(folder_id))

注冊中間件

  1. app = Sanic(__name__)

  2. @app.middleware

  3. async def halt_request(request):

  4.    print('I am a spy')

  5. @app.middleware('request')

  6. async def halt_request(request):

  7.    return text('I halted the request')

  8. @app.middleware('response')

  9. async def halt_response(request, response):

  10.    return text('I halted the response')

  11. @app.route('/')

  12. async def handler(request):

  13.    return text('I would like to speak now please')

  14. app.run(host='0.0.0.0', port=8000)

異常處理

拋出異常

  1. from sanic import Sanic

  2. from sanic.exceptions import ServerError

  3. @app.route('/killme')

  4. def i_am_ready_to_die(request):

  5.    raise ServerError('Something bad happened')

處理異常

  1. from sanic import Sanic

  2. from sanic.response import text

  3. from sanic.exceptions import NotFound

  4. @app.exception(NotFound)

  5. def ignore_404s(request, exception):

  6.    return text('Yep, I totally found the page: {}'.format(request.url))

藍圖

和flask中的藍圖一樣,用于組織項目結(jié)構(gòu) 
創(chuàng)建一個藍圖,相當于創(chuàng)建一個sanic app,上面的用法和上面相同,把app改成藍圖名稱bp

  1. from sanic.response import json

  2. from sanic import Blueprint

  3. bp = Blueprint('my_blueprint')

  4. @bp.route('/')

  5. async def bp_root():

  6.    return json({'my': 'blueprint'})

藍圖注冊到主app

  1. from sanic import Sanic

  2. from my_blueprint import bp

  3. app = Sanic(__name__)

  4. app.register_blueprint(bp)

  5. app.run(host='0.0.0.0', port=8000, debug=True)

總結(jié)

sanic將是一個非常流行的框架.因為它基于python3.5+,使用了許多新的特性,這些特性讓程序速度更快。

作者:Prasanta 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多