成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

sanic中文文檔

?。?。 / 2015人閱讀

摘要:入門指南路由路由允許用戶為不同的端點指定處理程序函數(shù)。被訪問服務器的基本,最終被路由器匹配到處理程序函數(shù),測試,然后返回一個對象。請求參數(shù)將作為關(guān)鍵字參數(shù)傳遞給路線處理程序函數(shù)。例如所有有效的參數(shù)必須傳遞給以便構(gòu)建一個。

入門指南

Install Sanic:python3 -m pip install sanic
example

from sanic import Sanic
from sanic.response import text
app = Sanic(__name__)
@app.route("/")
async def test(request):
    return text("Hello world!")
app.run(host="0.0.0.0", port=8000, debug=True)
路由

路由允許用戶為不同的URL端點指定處理程序函數(shù)。

demo:

from sanic.response import json
@app.route("/")
async def test(request):
    return json({ "hello": "world" })

url http://server.url/ 被訪問(服務器的基本url),最終"/"被路由器匹配到處理程序函數(shù),測試,然后返回一個JSON對象。

請求參數(shù) 請求參數(shù)

要指定一個參數(shù),可以用像這樣的角引號包圍它。請求參數(shù)將作為關(guān)鍵字參數(shù)傳遞給路線處理程序函數(shù)。
demo

from sanic.response import text
@app.route("/tag/")
async def tag_handler(request, tag):
    return text("Tag - {}".format(tag))

為參數(shù)指定類型,在參數(shù)名后面添加(:類型)。如果參數(shù)不匹配指定的類型,Sanic將拋出一個不存在的異常,導致一個404頁面
demo:

from sanic.response import text
@app.route("/number/")
async def integer_handler(request, integer_arg):
    return text("Integer - {}".format(integer_arg))
@app.route("/number/")
async def number_handler(request, number_arg):
    return text("Number - {}".format(number_arg))
@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))
請求類型
路由裝飾器接受一個可選的參數(shù),方法,它允許處理程序函數(shù)與列表中的任何HTTP方法一起工作。
demo_1
from sanic.response import text
@app.route("/post", methods=["POST"])
async def post_handler(request):
    return text("POST request - {}".format(request.json))
@app.route("/get", methods=["GET"])
async def get_handler(request):
    return text("GET request - {}".format(request.args))
demo_2
from sanic.response import text
@app.post("/post")
async def post_handler(request):
    return text("POST request - {}".format(request.json))
@app.get("/get")
async def get_handler(request):
    return text("GET request - {}".format(request.args))
增加路由
from sanic.response import text
# Define the handler functions
async def handler1(request):
    return text("OK")
async def handler2(request, name):
    return text("Folder - {}".format(name))
async def person_handler2(request, name):
    return text("Person - {}".format(name))
# Add each handler function as a route
app.add_route(handler1, "/test")
app.add_route(handler2, "/folder/")
app.add_route(person_handler2, "/person/", methods=["GET"])
url_for

Sanic提供了一個urlfor方法,根據(jù)處理程序方法名生成url。避免硬編碼url路徑到您的應用程序
demo

@app.route("/")
async def index(request):
    # generate a URL for the endpoint `post_handler`
    url = app.url_for("post_handler", post_id=5)
    # the URL is `/posts/5`, redirect to it
    return redirect(url)
@app.route("/posts/")
async def post_handler(request, post_id):
    return text("Post - {}".format(post_id))

Notice:

給url equest的關(guān)鍵字參數(shù)不是請求參數(shù),它將包含在URL的查詢字符串中。例如:

url = app.url_for("post_handler", post_id=5, arg_one="one", arg_two="two")
# /posts/5?arg_one=one&arg_two=two

所有有效的參數(shù)必須傳遞給url以便構(gòu)建一個URL。如果沒有提供一個參數(shù),或者一個參數(shù)與指定的類型不匹配,就會拋出一個URLBuildError
可以將多值參數(shù)傳遞給url

url = app.url_for("post_handler", post_id=5, arg_one=["one", "two"])
# /posts/5?arg_one=one&arg_one=two
WebSocket routes(網(wǎng)絡(luò)套接字路由)

websocket 可以通過裝飾路由實現(xiàn)
demo:

@app.websocket("/feed")
async def feed(request, ws):
    while True:
        data = "hello!"
        print("Sending: " + data)
        await ws.send(data)
        data = await ws.recv()
        print("Received: " + data)
        
另外,添加 websocket 路由方法可以代替裝飾器

async def feed(request, ws):
    pass
app.add_websocket_route(my_websocket_handler, "/feed")
響應( response ) text
from sanic import response
@app.route("/text")
def handle_request(request):
    return response.text("Hello world!")
HTML
from sanic import response
@app.route("/html")
def handle_request(request):
    return response.html("

Hello world!

")
JSON
from sanic import response
@app.route("/json")
def handle_request(request):
    return response.json({"message": "Hello world!"})
File
from sanic import response
@app.route("/file")
async def handle_request(request):
    return await response.file("/srv/www/whatever.png")
Streaming
from sanic import response
@app.route("/streaming")
async def index(request):
    async def streaming_fn(response):
        response.write("foo")
        response.write("bar")
    return response.stream(streaming_fn, content_type="text/plain")
File Streaming

對于大文件,文件和流的組合

from sanic import response
@app.route("/big_file.png")
async def handle_request(request):
    return await response.file_stream("/srv/www/whatever.png")
Redirect
from sanic import response
@app.route("/redirect")
def handle_request(request):
    return response.redirect("/json")
Raw

沒有進行編碼的響應

from sanic import response
@app.route(‘/raw ’)
def handle_request(request):
return response.raw(‘ raw data ’)
Modify headers or status

要修改頭或狀態(tài)代碼,將標題或狀態(tài)參數(shù)傳遞給這些函數(shù)

from sanic import response
@app.route(‘/json ’)
def handle_request(request):
return response.json(
{‘ message ’: ‘ Hello world!’},
headers={‘ X-Served-By ’: ‘ sanic ’},
status=200
)

更多的內(nèi)容:Sanic 中文文檔

靜態(tài)文件

異常處理

中間件和監(jiān)聽

藍圖

配置

裝飾器

請求數(shù)據(jù)

試圖類

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

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/41000.html

相關(guān)文章

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

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

    booster 評論0 收藏0
  • sanic異步框架之中文文檔

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

    elliott_hu 評論0 收藏0
  • 微信公號DIY:一小時搭建微信聊天機器人

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

    source 評論0 收藏0
  • python 最快 web 框架 Sanci 快速入門

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

    snifes 評論0 收藏0
  • 使用Sanic開發(fā)快速異步響應的Web程序

    摘要:在類似的基礎(chǔ)上,支持異步請求處理,也就是說,你可以使用中全新而又亮眼的語法,使你的代碼非阻塞且快速。就是基于實現(xiàn)的異步讀寫的數(shù)據(jù)庫模塊,同樣有模塊為因一波封裝了,使得讀寫更加方便,它就是 Sanic是一個類似Flask、僅僅支持Python 3.5+ 版本的web 服務器,旨在運行速度更快。在類似Flask的基礎(chǔ)上,Sanic支持異步請求處理,也就是說,你可以使用Python 3.5 ...

    clasnake 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<