日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python web 框架例子_最快的 Python Web 框架入门

發布時間:2025/3/20 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python web 框架例子_最快的 Python Web 框架入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原標題:最快的 Python Web 框架入門

來源: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,寫入下面的內容

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數據

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+,使用了許多新的特性,這些特性讓程序速度更快。返回搜狐,查看更多

責任編輯:

總結

以上是生活随笔為你收集整理的python web 框架例子_最快的 Python Web 框架入门的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。