最快的 Python Web 框架入門

速度比較
最快的 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,寫入下面的內容
1.from sanic import Sanic
2.from sanic.response import json
3.
4.app = Sanic(name)br/>5.
[email protected]("/")
7.async def test(request):

  1.    return json({ "hello": "world" })
  2. 10.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表單數據
    例子
    1.from sanic import Sanic
    2.from sanic.response import json

  3. [email protected]("/json")
    5.def post_json(request):

  4.    return json({ "received": True, "message": request.json })
  5. [email protected]("/form")
    9.def post_json(request):

  6.    return json({ "received": True, "form_data": request.form, "test": request.form.get('test') })
  7. [email protected]("/files")
    13.def post_json(request):

  8.    test_file = request.files.get('test')
  9.    file_parameters = {
  10.        'body': test_file.body,
  11.        'name': test_file.name,
  12.        'type': test_file.type,
  13.    }
  14.    return json({ "received": True, "file_names": request.files.keys(), "test_file_parameters": file_parameters })
  15. [email protected]("/query_string")
    25.def query_string(request):

  16.    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
  17. [email protected]('/tag/')
    5.async def person_handler(request, tag):

  18.    return text('Tag - {}'.format(tag))
  19. [email protected]('/number/')
    9.async def person_handler(request, integer_arg):

  20.    return text('Integer - {}'.format(integer_arg))
  21. [email protected]('/number/')
    13.async def person_handler(request, number_arg):

  22.    return text('Number - {}'.format(number))
  23. [email protected]('/person/')
    17.async def person_handler(request, name):

  24.    return text('Person - {}'.format(name))
  25. [email protected]('/folder/')
    21.async def folder_handler(request, folder_id):

  26.    return text('Folder - {}'.format(folder_id))
    註冊中間件
    1.app = Sanic(name)
  27. [email protected]
    4.async def halt_request(request):

  28.    print("I am a spy")
  29. [email protected]('request')
    8.async def halt_request(request):

  30.    return text('I halted the request')
  31. [email protected]('response')
    12.async def halt_response(request, response):

  32.    return text('I halted the response')
  33. [email protected]('/')
    16.async def handler(request):

  34.    return text('I would like to speak now please')
  35. 19.app.run(host="0.0.0.0", port=8000)
    異常處理
    拋出異常
    1.from sanic import Sanic
    2.from sanic.exceptions import ServerError

  36. [email protected]('/killme')
    5.def i_am_ready_to_die(request):

  37.    raise ServerError("Something bad happened")
    處理異常
    1.from sanic import Sanic
    2.from sanic.response import text
    3.from sanic.exceptions import NotFoundbr/>[email protected](NotFound)
    5.def ignore_404s(request, exception):
  38.    return text("Yep, I totally found the page: {}".format(request.url))
    藍圖
    和flask中的藍圖一樣,用於組織項目結構 
    創建一個藍圖,相當於創建一個sanic app,上面的用法和上面相同,把app改成藍圖名稱bp
    1.from sanic.response import json
    2.from sanic import Blueprint
  39. 4.bp = Blueprint('my_blueprint')

  40. [email protected]('/')
    7.async def bp_root():

  41.    return json({'my': 'blueprint'})
    藍圖註冊到主app
    1.from sanic import Sanic
    2.from my_blueprint import bp
  42. 4.app = Sanic(name)
    5.app.register_blueprint(bp)

  43. 7.app.run(host='0.0.0.0', port=8000, debug=True)
    總結
    sanic將是一個非常流行的框架.因爲它基於python3.5+,使用了許多新的特性,這些特性讓程序速度更快。
    *聲明:推送內容及圖片來源於網絡,部分內容會有所改動,版權歸原作者所有,如來源信息有誤或侵犯權益,請聯繫我們刪除或授權事宜。
    最快的 Python Web 框架入門

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章