zoukankan      html  css  js  c++  java
  • Sanic官翻-流媒体

    流媒体

    请求流

    Sanic允许您按流获取请求数据,如下所示。请求结束时,await request.stream.read()返回None。只有post,put和patch装饰器具有stream参数。

    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(__name__)
    
    
    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 = await request.stream.read()
                if body is None:
                    break
                body = body.decode('utf-8').replace('1', 'A')
                await response.write(body)
        return stream(streaming)
    
    
    @bp.put('/bp_stream', stream=True)
    async def bp_put_handler(request):
        result = ''
        while True:
            body = await request.stream.read()
            if body is None:
                break
            result += body.decode('utf-8').replace('1', 'A')
        return text(result)
    
    
    # You can also use `bp.add_route()` with stream argument
    async def bp_post_handler(request):
        result = ''
        while True:
            body = await request.stream.read()
            if body is None:
                break
            result += body.decode('utf-8').replace('1', 'A')
        return text(result)
    
    bp.add_route(bp_post_handler, '/bp_stream', methods=['POST'], stream=True)
    
    
    async def post_handler(request):
        result = ''
        while True:
            body = await request.stream.read()
            if body is None:
                break
            result += body.decode('utf-8')
        return text(result)
    
    app.blueprint(bp)
    app.add_route(SimpleView.as_view(), '/method_view')
    view = CompositionView()
    view.add(['POST'], post_handler, stream=True)
    app.add_route(view, '/composition_view')
    
    
    if __name__ == '__main__':
        app.run(host='127.0.0.1', port=8000)
    

    响应流

    Sanic允许您使用stream方法将内容流式传输到客户端。此方法接受协程回调,该回调传递给写入的StreamingHTTPResponse对象。一个简单的例子如下:

    from sanic import Sanic
    from sanic.response import stream
    
    app = Sanic(__name__)
    
    @app.route("/")
    async def test(request):
        async def sample_streaming_fn(response):
            await response.write('foo,')
            await response.write('bar')
    
        return stream(sample_streaming_fn, content_type='text/csv')
    

    在要将内容流传输到源自外部服务(例如数据库)的客户端的情况下,这很有用。例如,您可以使用asyncpg提供的异步游标将数据库记录流式传输到客户端:

    @app.route("/")
    async def index(request):
        async def stream_from_db(response):
            conn = await asyncpg.connect(database='test')
            async with conn.transaction():
                async for record in conn.cursor('SELECT generate_series(0, 10)'):
                    await response.write(record[0])
    
        return stream(stream_from_db)
    

    如果客户端支持HTTP / 1.1,Sanic将使用分块传输编码;;您可以使用流功能的分块选项显式启用或禁用它。

    文件流

    Sanic提供sanic.response.file_stream函数,该函数在您要发送大文件时很有用。它返回一个StreamingHTTPResponse对象,默认情况下将使用分块传输编码。因此,Sanic不会在响应中添加Content-Length HTTP标头。如果要使用此标头,则可以禁用分块传输编码并手动添加它:

    from aiofiles import os as async_os
    from sanic.response import file_stream
    
    @app.route("/")
    async def index(request):
        file_path = "/srv/www/whatever.png"
    
        file_stat = await async_os.stat(file_path)
        headers = {"Content-Length": str(file_stat.st_size)}
    
        return await file_stream(
            file_path,
            headers=headers,
            chunked=False,
        )
    
  • 相关阅读:
    创业指南:如何实现打工族的老板梦
    在C#中压缩解压缩文件(适合.Net1.x)
    35岁之前成功的12条黄金法则
    郑州DOTNET俱乐部《DotNet实战之旅》活动邀请
    1baiwan.com你能走多远?(原创,请任意转载,作者:小张.net)
    MongoDB实战开发 【零基础学习,附完整Asp.net示例】
    TFS2010强制撤签锁定项
    持续集成理论和实践的新进展
    JQuery最佳实践
    IE下实现全屏两方法
  • 原文地址:https://www.cnblogs.com/fhkankan/p/14763524.html
Copyright © 2011-2022 走看看