专栏名称: 马哥Linux运维
马哥linux致力于linux运维培训,连续多年排名第一,订阅者可免费获得学习机会和相关Linux独家实战资料!
目录
相关文章推荐
运维  ·  再见,CDN 巨头:Akamai 宣布 ... ·  3 天前  
51好读  ›  专栏  ›  马哥Linux运维

最快的 Python Web 框架入门

马哥Linux运维  · 公众号  · 运维  · 2018-05-10 18:33

正文

来源: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,写入下面的内容

  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数据
request.args (dict) - get数据
request.form (dict) - post表单数据

例子

  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







请到「今天看啥」查看全文