Sanic框架流式傳輸操作示例
本文實例講述了Sanic框架流式傳輸操作。分享給大家供大家參考,具體如下:
簡介
Sanic是一個類似Flask的Python 3.5+ Web服務(wù)器,它的寫入速度非常快。除了Flask之外,Sanic還支持異步請求處理程序。這意味著你可以使用Python 3.5中新的閃亮的異步/等待語法,使你的代碼非阻塞和快速。
在前面一篇《Sanic框架Cookies操作》中已經(jīng)講到,如何在Sanic中使用Cookie,接下來將介紹一下Sanic的流的使用:
請求流式傳輸
Sanic允許通過流獲取請求數(shù)據(jù),如下所示,當(dāng)請求結(jié)束時,request.stream.get()返回為None,只有post、put和patch decorator擁有流參數(shù):
from sanic.response import stream
@app.post("/post_stream",stream=True)
async def post_stream(request):
async def streaming(response):
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode("utf-8")
reponse.write(body)
return stream(streaming)
@app.put("/put_stream",stream=True)
async def put_stream(request):
async def streaming(response):
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode("utf-8")
response.write("utf-8")
return stream(streaming)
除了上述例子的方法之外,我們之前還講過用add_route方法動態(tài)添加路由:
from sanic.response import text
from sanic.views import HTTPMethodView
from sanic.views import stream as stream_decorator
class StreamView(HTTPMethodView)
@stream_decorator
async def post(self,request)
result = ''
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode('utf-8')
result += body
return text(result)
app.add_route(StreamView.as_view(),"/method_view")
值得注意的是,stream_decorator裝飾器中處理函數(shù)的函數(shù)名稱,若為post則為post請求,若為put則為put請求。在之前講述路由的文章《Sanic框架路由用法》中講到一個CompositionView類來自定義一個路由,CompositionView在流式請求中同樣適用:
from sanic.views import CompositionView
async def post_stream_view(request):
result = ''
while True:
body = await request.stream.get()
if body is None:
break
body = body.decode('utf-8')
result += body
return text(result)
view = CompositionView()
view.add(['POST'],post_stream_view,stream=True)
app.add_route(view,"/post_stream_view")
響應(yīng)流式傳輸
Sanic允許你使用stream方法將內(nèi)容傳輸?shù)娇蛻舳?,該方法接受一個通過StreamingHTTPResponse傳入的對象的協(xié)程回調(diào),舉個栗子:
from sanic.response import stream
@app.route("/post_stream_info",methods=["POST"])
async def post_stream_info(request):
async def streaming(response):
response.write("no")
response.write("bug")
return stream(streaming)
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python入門與進階經(jīng)典教程》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))
這篇文章主要給大家介紹了關(guān)于利用scrapy將爬到的數(shù)據(jù)保存到mysql(防止重復(fù))的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2018-03-03
Python利用scikit-learn實現(xiàn)近鄰算法分類的示例詳解
scikit-learn已經(jīng)封裝好很多數(shù)據(jù)挖掘的算法,這篇文章就來用scikit-learn實現(xiàn)近鄰算法分類,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-02-02
Django學(xué)習(xí)之路之請求與響應(yīng)
這篇文章主要為大家詳細介紹了Django的請求與響應(yīng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法
這篇文章主要介紹了python中如何實現(xiàn)將數(shù)據(jù)分成訓(xùn)練集與測試集的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
詳談Python高階函數(shù)與函數(shù)裝飾器(推薦)
下面小編就為大家?guī)硪黄斦凱ython高階函數(shù)與函數(shù)裝飾器(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
在Python中使用matplotlib模塊繪制數(shù)據(jù)圖的示例
這篇文章主要介紹了在Python中使用matplotlib模塊繪制數(shù)據(jù)圖的示例,matplotlib模塊經(jīng)常被用來實現(xiàn)數(shù)據(jù)的可視化,需要的朋友可以參考下2015-05-05

