成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

基于python3.5+的web框架sanic中文入門教程

booster / 1736人閱讀

摘要:簡(jiǎn)介是一款用寫的,用法和類似,的特點(diǎn)是非??旃倬W(wǎng)速度比較框架實(shí)現(xiàn)基礎(chǔ)每秒請(qǐng)求數(shù)平均時(shí)間安裝環(huán)境創(chuàng)建文件,寫入下面的內(nèi)容運(yùn)行是不是看起來和一樣屬性上傳文件列表數(shù)據(jù)數(shù)據(jù)表單數(shù)據(jù)例子路由和差不多,一看就懂注冊(cè)中間件異常處

簡(jiǎn)介

sanic是一款用python3.5+寫的web framework,用法和flask類似,sanic的特點(diǎn)是非???/strong>
github官網(wǎng):https://github.com/channelcat...

速度比較
框架 實(shí)現(xiàn)基礎(chǔ) 每秒請(qǐng)求數(shù) 平均時(shí)間
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
安裝

環(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)

運(yùn)行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))
注冊(cè)中間件
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)圖一樣,用于組織項(xiàng)目結(jié)構(gòu)
創(chuàng)建一個(gè)藍(lán)圖,相當(dāng)于創(chuàng)建一個(gè)sanic app,上面的用法和上面相同,把a(bǔ)pp改成藍(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)圖注冊(cè)到主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將是一個(gè)非常流行的框架.因?yàn)樗趐ython3.5+,使用了許多新的特性,這些特性讓程序速度更快.

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/38221.html

相關(guān)文章

  • 微信公號(hào)DIY:一小時(shí)搭建微信聊天機(jī)器人

    摘要:最近借用了女朋友的公號(hào),感覺如果只是用來發(fā)文章,太浪費(fèi)微信給提供的這些功能了。想了想,先從最簡(jiǎn)單的開始,做一個(gè)聊天機(jī)器人吧。是一款接口的,基于一系列規(guī)則和機(jī)器學(xué)習(xí)算法完成的聊天機(jī)器人。 最近借用了女朋友的公號(hào),感覺如果只是用來發(fā)文章,太浪費(fèi)微信給提供的這些功能了。想了想,先從最簡(jiǎn)單的開始,做一個(gè)聊天機(jī)器人吧。 使用Python實(shí)現(xiàn)聊天機(jī)器人的方案有多種:AIML、chatterBot以...

    source 評(píng)論0 收藏0
  • python 最快 web 框架 Sanci 快速入門

    摘要:詳細(xì)信息可以看下這個(gè)問題先在說下我的部署方式使用部署配置文件啟動(dòng)方式總結(jié)試用了下,把之前的一個(gè)聊天機(jī)器人從改成了。預(yù)告下一篇將介紹如何使用一步一步創(chuàng)建一個(gè)聊天機(jī)器人。 簡(jiǎn)介 Sanic 是一個(gè)和類Flask 的基于Python3.5+的web框架,它編寫的代碼速度特別快。除了像Flask 以外,Sanic 還支持以異步請(qǐng)求的方式處理請(qǐng)求。這意味著你可以使用新的 async/await ...

    snifes 評(píng)論0 收藏0
  • Sanic教程:快速開始

    摘要:快速開始在安裝之前在支持異步的過程中,都經(jīng)歷了哪些比較重大的更新。踏出第一步我們將正式使用來構(gòu)建一個(gè)項(xiàng)目,讓我們踏出第一步,利用來編寫一個(gè)返回字符串的服務(wù)程序。本次示例的源代碼全部在上,見。 快速開始 在安裝Sanic之前,讓我們一起來看看Python在支持異步的過程中,都經(jīng)歷了哪些比較重大的更新。 首先是Python3.4版本引入了asyncio,這讓Python有了支持異步IO的標(biāo)...

    warmcheng 評(píng)論0 收藏0
  • sanic異步框架中文文檔

    摘要:實(shí)例實(shí)例測(cè)試結(jié)果增加路由實(shí)例測(cè)試結(jié)果提供了一個(gè)方法,根據(jù)處理程序方法名生成。異常拋出異常要拋出異常,只需從異常模塊中提出相應(yīng)的異常。 typora-copy-images-to: ipic [TOC] 快速開始 在安裝Sanic之前,讓我們一起來看看Python在支持異步的過程中,都經(jīng)歷了哪些比較重大的更新。 首先是Python3.4版本引入了asyncio,這讓Python有了支...

    elliott_hu 評(píng)論0 收藏0
  • Python web 框架Sanic 學(xué)習(xí): 自定義 Exception

    摘要:是一個(gè)和類的基于的框架,它使用了異步特性,有遠(yuǎn)超的性能。那我們能不能自定義異常處理方法呢答案當(dāng)然是可以。提供了的響應(yīng)對(duì)象。以下自定義的異常處理類無效的使用捕獲異常,返回自定義響應(yīng)數(shù)據(jù)參考鏈接最后,感謝女朋友支持。 Sanic 是一個(gè)和類Flask 的基于Python3.5+的web框架,它使用了 Python3 異步特性,有遠(yuǎn)超 flask 的性能。 編寫 RESTful API 的時(shí)...

    2i18ns 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<