当前位置: 首页>>代码示例>>Python>>正文


Python response.stream方法代码示例

本文整理汇总了Python中sanic.response.stream方法的典型用法代码示例。如果您正苦于以下问题:Python response.stream方法的具体用法?Python response.stream怎么用?Python response.stream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sanic.response的用法示例。


在下文中一共展示了response.stream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_request_stream_handle_exception

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def test_request_stream_handle_exception(app):
    """for handling exceptions properly"""

    @app.post("/post/<id>", stream=True)
    async def post(request, id):
        assert isinstance(request.stream, StreamBuffer)
        result = ""
        while True:
            body = await request.stream.read()
            if body is None:
                break
            result += body.decode("utf-8")
        return text(result)

    # 404
    request, response = app.test_client.post("/in_valid_post", data=data)
    assert response.status == 404
    assert "Requested URL /in_valid_post not found" in response.text

    # 405
    request, response = app.test_client.get("/post/random_id")
    assert response.status == 405
    assert "Method GET not allowed for URL /post/random_id" in response.text 
开发者ID:huge-success,项目名称:sanic,代码行数:25,代码来源:test_request_stream.py

示例2: stream_response

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def stream_response(
        self,
        on_new_message: Callable[[UserMessage], Awaitable[None]],
        text: Text,
        sender_id: Text,
        input_channel: Text,
        metadata: Optional[Dict[Text, Any]],
    ) -> Callable[[Any], Awaitable[None]]:
        async def stream(resp: Any) -> None:
            q = Queue()
            task = asyncio.ensure_future(
                self.on_message_wrapper(
                    on_new_message, text, q, sender_id, input_channel, metadata
                )
            )
            result = None  # declare variable up front to avoid pytype error
            while True:
                result = await q.get()
                if result == "DONE":
                    break
                else:
                    await resp.write(json.dumps(result) + "\n")
            await task

        return stream  # pytype: disable=bad-return-type 
开发者ID:botfront,项目名称:rasa-for-botfront,代码行数:27,代码来源:channel.py

示例3: handler_stream

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def handler_stream(request):
    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 response.stream(body) 
开发者ID:huge-success,项目名称:sanic,代码行数:10,代码来源:run_asgi.py

示例4: post

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def post(self, request):
        result = ''
        while True:
            body = await request.stream.get()
            if body is None:
                break
            result += body.decode('utf-8')
        return text(result) 
开发者ID:huge-success,项目名称:sanic,代码行数:10,代码来源:server.py

示例5: handler

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def handler(request):
    async def streaming(response):
        while True:
            body = await request.stream.get()
            if body is None:
                break
            body = body.decode('utf-8').replace('1', 'A')
            await response.write(body)
    return stream(streaming) 
开发者ID:huge-success,项目名称:sanic,代码行数:11,代码来源:server.py

示例6: bp_handler

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def bp_handler(request):
    result = ''
    while True:
        body = await request.stream.get()
        if body is None:
            break
        result += body.decode('utf-8').replace('1', 'A')
    return text(result) 
开发者ID:huge-success,项目名称:sanic,代码行数:10,代码来源:server.py

示例7: post_handler

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def post_handler(request):
    result = ''
    while True:
        body = await request.stream.get()
        if body is None:
            break
        result += body.decode('utf-8')
    return text(result) 
开发者ID:huge-success,项目名称:sanic,代码行数:10,代码来源:server.py

示例8: test_request_stream_100_continue

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def test_request_stream_100_continue(app, headers, expect_raise_exception):
    class SimpleView(HTTPMethodView):
        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, StreamBuffer)
            result = ""
            while True:
                body = await request.stream.read()
                if body is None:
                    break
                result += body.decode("utf-8")
            return text(result)

    app.add_route(SimpleView.as_view(), "/method_view")

    assert app.is_request_stream is True

    if not expect_raise_exception:
        request, response = app.test_client.post(
            "/method_view", data=data, headers={"EXPECT": "100-continue"}
        )
        assert response.status == 200
        assert response.text == data
    else:
        with pytest.raises(ValueError) as e:
            app.test_client.post(
                "/method_view",
                data=data,
                headers={"EXPECT": "100-continue-extra"},
            )
            assert "Unknown Expect: 100-continue-extra" in str(e) 
开发者ID:huge-success,项目名称:sanic,代码行数:33,代码来源:test_request_stream.py

示例9: test_request_stream_composition_view

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def test_request_stream_composition_view(app):
    """for self.is_request_stream = True"""

    def get_handler(request):
        assert request.stream is None
        return text("OK")

    async def post_handler(request):
        assert isinstance(request.stream, StreamBuffer)
        result = ""
        while True:
            body = await request.stream.read()
            if body is None:
                break
            result += body.decode("utf-8")
        return text(result)

    view = CompositionView()
    view.add(["GET"], get_handler)
    view.add(["POST"], post_handler, stream=True)
    app.add_route(view, "/composition_view")

    assert app.is_request_stream is True

    request, response = app.test_client.get("/composition_view")
    assert response.status == 200
    assert response.text == "OK"

    request, response = app.test_client.post("/composition_view", data=data)
    assert response.status == 200
    assert response.text == data 
开发者ID:huge-success,项目名称:sanic,代码行数:33,代码来源:test_request_stream.py

示例10: test_streaming_new_api

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def test_streaming_new_api(app):
    @app.post("/non-stream")
    async def handler(request):
        assert request.body == b"x"
        await request.receive_body()  # This should do nothing
        assert request.body == b"x"
        return text("OK")

    @app.post("/1", stream=True)
    async def handler(request):
        assert request.stream
        assert not request.body
        await request.receive_body()
        return text(request.body.decode().upper())

    @app.post("/2", stream=True)
    async def handler(request):
        ret = []
        async for data in request.stream:
            # We should have no b"" or None, just proper chunks
            assert data
            assert isinstance(data, bytes)
            ret.append(data.decode("ASCII"))
        return json(ret)

    request, response = app.test_client.post("/non-stream", data="x")
    assert response.status == 200

    request, response = app.test_client.post("/1", data="TEST data")
    assert request.body == b"TEST data"
    assert response.status == 200
    assert response.text == "TEST DATA"

    request, response = app.test_client.post("/2", data=data)
    assert response.status == 200
    res = response.json
    assert isinstance(res, list)
    assert len(res) > 1
    assert "".join(res) == data 
开发者ID:huge-success,项目名称:sanic,代码行数:41,代码来源:test_request_stream.py

示例11: test_stream_request_cancel_when_conn_lost

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def test_stream_request_cancel_when_conn_lost(app):
    app.still_serving_cancelled_request = False

    @app.post("/post/<id>", stream=True)
    async def post(request, id):
        assert isinstance(request.stream, asyncio.Queue)

        async def streaming(response):
            while True:
                body = await request.stream.get()
                if body is None:
                    break
                await response.write(body.decode("utf-8"))

        await asyncio.sleep(1.0)
        # at this point client is already disconnected
        app.still_serving_cancelled_request = True

        return stream(streaming)

    # schedule client call
    loop = asyncio.get_event_loop()
    task = loop.create_task(app.asgi_client.post("/post/1"))
    loop.call_later(0.01, task)
    await asyncio.sleep(0.5)

    # cancelling request and closing connection after 0.5 sec
    task.cancel()

    with contextlib.suppress(asyncio.CancelledError):
        await task

    # Wait for server and check if it's still serving the cancelled request
    await asyncio.sleep(1.0)

    assert app.still_serving_cancelled_request is False 
开发者ID:huge-success,项目名称:sanic,代码行数:38,代码来源:test_request_cancel.py

示例12: stream_response

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def stream_response(self, on_new_message, text, sender_id):
        async def stream(resp):
            q = Queue()
            task = asyncio.ensure_future(
                self.on_message_wrapper(on_new_message, text, q, sender_id))
            while True:
                result = await q.get()
                if result == "DONE":
                    break
                else:
                    await resp.write(json.dumps(result) + "\n")
            await task

        return stream 
开发者ID:RasaHQ,项目名称:rasa_core,代码行数:16,代码来源:channel.py

示例13: blueprint

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def blueprint(self, on_new_message):
        custom_webhook = Blueprint(
            'custom_webhook_{}'.format(type(self).__name__),
            inspect.getmodule(self).__name__)

        # noinspection PyUnusedLocal
        @custom_webhook.route("/", methods=['GET'])
        async def health(request):
            return response.json({"status": "ok"})

        @custom_webhook.route("/webhook", methods=['POST'])
        async def receive(request):
            sender_id = await self._extract_sender(request)
            text = self._extract_message(request)
            should_use_stream = utils.bool_arg(request, "stream",
                                               default=False)

            if should_use_stream:
                return response.stream(
                    self.stream_response(on_new_message, text, sender_id),
                    content_type='text/event-stream')
            else:
                collector = CollectingOutputChannel()
                # noinspection PyBroadException
                try:
                    await on_new_message(UserMessage(text, collector, sender_id,
                                                     input_channel=self.name()))
                except CancelledError:
                    logger.error("Message handling timed out for "
                                 "user message '{}'.".format(text))
                except Exception:
                    logger.exception("An exception occured while handling "
                                     "user message '{}'.".format(text))
                return response.json(collector.messages)

        return custom_webhook 
开发者ID:RasaHQ,项目名称:rasa_core,代码行数:38,代码来源:channel.py

示例14: notification

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def notification(request):
    async def _stream(res):
        redis = aredis.StrictRedis()
        pub = redis.pubsub()
        await pub.subscribe('test')
        end_time = app.loop.time() + 30
        while app.loop.time() < end_time:
            await redis.publish('test', 111)
            message = None
            while not message:
                message = await pub.get_message()
            res.write(message)
            await asyncio.sleep(0.1)
    return stream(_stream) 
开发者ID:NoneGG,项目名称:aredis,代码行数:16,代码来源:sanic_server.py

示例15: blueprint

# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import stream [as 别名]
def blueprint(
        self, on_new_message: Callable[[UserMessage], Awaitable[None]]
    ) -> Blueprint:
        custom_webhook = Blueprint(
            "custom_webhook_{}".format(type(self).__name__),
            inspect.getmodule(self).__name__,
        )

        # noinspection PyUnusedLocal
        @custom_webhook.route("/", methods=["GET"])
        async def health(request: Request) -> HTTPResponse:
            return response.json({"status": "ok"})

        @custom_webhook.route("/webhook", methods=["POST"])
        async def receive(request: Request) -> HTTPResponse:
            sender_id = await self._extract_sender(request)
            text = self._extract_message(request)
            should_use_stream = rasa.utils.endpoints.bool_arg(
                request, "stream", default=False
            )
            input_channel = self._extract_input_channel(request)
            metadata = self.get_metadata(request)

            if should_use_stream:
                return response.stream(
                    self.stream_response(
                        on_new_message, text, sender_id, input_channel, metadata
                    ),
                    content_type="text/event-stream",
                )
            else:
                collector = CollectingOutputChannel()
                # noinspection PyBroadException
                try:
                    await on_new_message(
                        UserMessage(
                            text,
                            collector,
                            sender_id,
                            input_channel=input_channel,
                            metadata=metadata,
                        )
                    )
                except CancelledError:
                    logger.error(
                        "Message handling timed out for "
                        "user message '{}'.".format(text)
                    )
                except Exception:
                    logger.exception(
                        "An exception occured while handling "
                        "user message '{}'.".format(text)
                    )
                return response.json(collector.messages)

        return custom_webhook 
开发者ID:botfront,项目名称:rasa-for-botfront,代码行数:58,代码来源:channel.py


注:本文中的sanic.response.stream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。