14. 流
你的鼓励是我前进的动力,请为我点个赞吧!
(1)请求流
sanic允许以流媒体的形式获取请求数据,当请求结束后await
request.stream.read()返回的参数为none,只有请求方式为post或者patch时才会设置相关参数。
from sanic import Sanic
from sanic.views import CompositionView
from sanic.views import HTTPMethodView
from sanic.views import stream as stream_decorator
from sanic.blueprints import Blueprint
from sanic.response import stream, text
bp = Blueprint('blueprint_request_stream')
app = Sanic('request_stream')
class SimpleView(HTTPMethodView):
@stream_decorator
async def post(self, request):
result = ''
while True:
body = await request.stream.read()
if body is None:
break
result += body.decode('utf-8')
return text(result)
@app.post('/stream', stream=True)
async def handler(request):
async def streaming(response):
while True:
body =