Python aiohttp.web.StreamResponse() Examples

The following are 30 code examples of aiohttp.web.StreamResponse(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module aiohttp.web , or try the search function .
Example #1
Source File: test_response_types.py    From aiohttp-session with Apache License 2.0 7 votes vote down vote up
def test_stream_response(aiohttp_client):

    async def stream_response(request):
        session = await get_session(request)
        session['will_not'] = 'show up'
        return web.StreamResponse()

    client = await aiohttp_client(create_app(('/stream', stream_response)))

    resp = await client.get('/stream')
    assert resp.status == 200
    assert SESSION_KEY.upper() not in resp.cookies 
Example #2
Source File: middlewares.py    From aiohttp_apiset with Apache License 2.0 6 votes vote down vote up
def __call__(self, app, handler):
        async def process(request):
            try:
                response = await handler(request)
            except web.HTTPException as ex:
                return self.resolve_exception(ex)
            else:
                if isinstance(response, asyncio.Future):
                    response = await response
                if isinstance(response, dict):
                    status = response.get('status', 200)
                    if not isinstance(status, int):
                        status = 200
                    return self.response(response, status=status)
                elif not isinstance(response, web.StreamResponse):
                    return self.response(response)
                return response
        return process 
Example #3
Source File: eventsource.py    From sockjs with Apache License 2.0 6 votes vote down vote up
def process(self):
        headers = (
            (hdrs.CONTENT_TYPE, "text/event-stream"),
            (hdrs.CACHE_CONTROL, CACHE_CONTROL),
        )
        headers += session_cookie(self.request)

        # open sequence (sockjs protocol)
        resp = self.response = web.StreamResponse(headers=headers)
        await resp.prepare(self.request)
        await resp.write(b"\r\n")

        # handle session
        await self.handle_session()

        return resp 
Example #4
Source File: xhr.py    From sockjs with Apache License 2.0 6 votes vote down vote up
def process(self):
        request = self.request

        if request.method == hdrs.METH_OPTIONS:
            headers = (
                (hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
                (hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),
            )
            headers += session_cookie(request)
            headers += cors_headers(request.headers)
            headers += cache_headers()
            return web.Response(status=204, headers=headers)

        headers = (
            (hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
            (hdrs.CACHE_CONTROL, CACHE_CONTROL),
        )
        headers += session_cookie(request)
        headers += cors_headers(request.headers)

        resp = self.response = web.StreamResponse(headers=headers)
        await resp.prepare(request)

        await self.handle_session()
        return resp 
Example #5
Source File: managed_stream.py    From lbry-sdk with MIT License 6 votes vote down vote up
def __init__(self, loop: asyncio.AbstractEventLoop, config: 'Config', blob_manager: 'BlobManager',
                 sd_hash: str, download_directory: Optional[str] = None, file_name: Optional[str] = None,
                 status: Optional[str] = ManagedDownloadSource.STATUS_STOPPED,
                 claim: Optional[StoredContentClaim] = None,
                 download_id: Optional[str] = None, rowid: Optional[int] = None,
                 descriptor: Optional[StreamDescriptor] = None,
                 content_fee: Optional['Transaction'] = None,
                 analytics_manager: Optional['AnalyticsManager'] = None,
                 added_on: Optional[int] = None):
        super().__init__(loop, config, blob_manager.storage, sd_hash, file_name, download_directory, status, claim,
                         download_id, rowid, content_fee, analytics_manager, added_on)
        self.blob_manager = blob_manager
        self.purchase_receipt = None
        self.downloader = StreamDownloader(self.loop, self.config, self.blob_manager, sd_hash, descriptor)
        self.analytics_manager = analytics_manager

        self.reflector_progress = 0
        self.uploading_to_reflector = False
        self.file_output_task: typing.Optional[asyncio.Task] = None
        self.delayed_stop_task: typing.Optional[asyncio.Task] = None
        self.streaming_responses: typing.List[typing.Tuple[Request, StreamResponse]] = []
        self.fully_reflected = asyncio.Event(loop=self.loop)
        self.streaming = asyncio.Event(loop=self.loop)
        self._running = asyncio.Event(loop=self.loop) 
Example #6
Source File: jinja2.py    From aiohttp_apiset with Apache License 2.0 5 votes vote down vote up
def template(template_name, *, app_key=APP_KEY, encoding='utf-8', status=200):
    """
    Decorator compatible with aiohttp_apiset router
    """

    def wrapper(func):
        @functools.wraps(func)
        async def wrapped(*args, **kwargs):
            if asyncio.iscoroutinefunction(func):
                coro = func
            else:
                coro = asyncio.coroutine(func)
            context = await coro(*args, **kwargs)
            if isinstance(context, web.StreamResponse):
                return context

            if 'request' in kwargs:
                request = kwargs['request']
            elif not args:
                request = None
                warnings.warn("Request not detected")
            elif isinstance(args[0], AbstractView):
                request = args[0].request
            else:
                request = args[-1]

            response = render_template(template_name, request, context,
                                       app_key=app_key, encoding=encoding)
            response.set_status(status)
            return response
        return wrapped
    return wrapper 
Example #7
Source File: test_transport.py    From sockjs with Apache License 2.0 5 votes vote down vote up
def test_handle_session_closed(make_transport, make_fut):
    trans = make_transport()
    trans.send = make_fut(1)
    trans.session.interrupted = False
    trans.session.state = protocol.STATE_CLOSED
    trans.session._remote_closed = make_fut(1)
    trans.response = web.StreamResponse()
    await trans.handle_session()
    trans.session._remote_closed.assert_called_with()
    trans.send.assert_called_with('c[3000,"Go away!"]') 
Example #8
Source File: rest_manager.py    From py-ipv8 with GNU Lesser General Public License v3.0 5 votes vote down vote up
def cors_middleware(request, handler):
    preflight_cors = request.method == "OPTIONS" and 'Access-Control-Request-Method' in request.headers
    if not preflight_cors:
        return await handler(request)

    response = web.StreamResponse()
    # For now, just allow all methods
    response.headers['Access-Control-Allow-Methods'] = "GET, PUT, POST, PATCH, DELETE, OPTIONS"
    response.headers['Access-Control-Allow-Headers'] = '*'
    response.headers['Access-Control-Allow-Origin'] = '*'
    response.headers['Access-Control-Max-Age'] = str(86400)
    return response 
Example #9
Source File: middleware.py    From website with MIT License 5 votes vote down vote up
def request_context_middleware(
    request: web.Request,
    handler: typing.Callable[[web.Request], typing.Awaitable[web.StreamResponse]],
) -> web.StreamResponse:
    """
    Middleware to set a context variable for the request/response cycle.
    """
    token = REQUEST_CONTEXT.set(uuid4())
    try:
        return await handler(request)
    finally:
        REQUEST_CONTEXT.reset(token) 
Example #10
Source File: web.py    From kube-web-view with GNU General Public License v3.0 5 votes vote down vote up
def download_tsv(request, table):
    response = web.StreamResponse()
    response.content_type = "text/tab-separated-values; charset=utf-8"
    path = request.rel_url.path
    filename = path.strip("/").replace("/", "_")
    response.headers["Content-Disposition"] = f'attachment; filename="{filename}.tsv"'
    await response.prepare(request)
    await as_tsv(table, ResponseWriter(response))
    return response 
Example #11
Source File: web.py    From kube-web-view with GNU General Public License v3.0 5 votes vote down vote up
def download_yaml(request, resource):
    response = web.StreamResponse()
    response.content_type = "text/vnd.yaml; charset=utf-8"
    path = request.rel_url.path
    filename = path.strip("/").replace("/", "_")
    response.headers["Content-Disposition"] = f'attachment; filename="{filename}.yaml"'
    await response.prepare(request)
    data = yaml.dump(resource.obj, default_flow_style=False)
    await response.write(data.encode("utf-8"))
    return response 
Example #12
Source File: rest_validr.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def decorate(self, f):
        assert inspect.iscoroutinefunction(f), f'{f} is not coroutine function'
        params = get_params(f)
        if params is not None:
            params = self._schema_compiler.compile(params)
        returns = get_returns(f)
        if returns is not None:
            returns = self._schema_compiler.compile(returns)

        async def wrapped(request, **kwargs):
            ret = None
            if params is not None:
                maps = [kwargs, request.match_info]
                if request.method in ['GET', 'DELETE']:
                    maps.append(request.query)
                else:
                    try:
                        maps.append(await request.json())
                    except json.JSONDecodeError:
                        return json_response({"message": 'Invalid JSON'}, status=400)
                try:
                    kwargs = params(ChainMap(*maps))
                except Invalid as ex:
                    ret = self._response_from_invalid(ex)
            if ret is None:
                ret = await f(request, **kwargs)
            if returns is not None:
                if not isinstance(ret, StreamResponse):
                    ret = returns(ret)
                    ret = json_response(ret)
            elif ret is None:
                ret = Response(status=204)
            return ret
        wrapped.__name__ = f.__name__
        wrapped.__qualname__ = f.__qualname__
        wrapped.__doc__ = f.__doc__
        return wrapped 
Example #13
Source File: swagger.py    From aiohttp-swagger3 with Apache License 2.0 5 votes vote down vote up
def _handle_swagger_call(
        self, route: "SwaggerRoute", request: web.Request
    ) -> web.StreamResponse:
        kwargs = await route.parse(request)
        return await route.handler(**kwargs) 
Example #14
Source File: swagger.py    From aiohttp-swagger3 with Apache License 2.0 5 votes vote down vote up
def _handle_swagger_method_call(
        self, view: web.View, route: "SwaggerRoute"
    ) -> web.StreamResponse:
        kwargs = await route.parse(view.request)
        return await route.handler(view, **kwargs) 
Example #15
Source File: test_main.py    From aiohttp-cors with Apache License 2.0 5 votes vote down vote up
def handler(request: web.Request) -> web.StreamResponse:
    """Dummy request handler, returning `TEST_BODY`."""
    response = web.Response(text=TEST_BODY)

    response.headers[SERVER_CUSTOM_HEADER_NAME] = SERVER_CUSTOM_HEADER_VALUE

    return response 
Example #16
Source File: test_main.py    From aiohttp-cors with Apache License 2.0 5 votes vote down vote up
def get(self) -> web.StreamResponse:
        """Dummy request handler, returning `TEST_BODY`."""
        response = web.Response(text=TEST_BODY)

        response.headers[SERVER_CUSTOM_HEADER_NAME] = \
            SERVER_CUSTOM_HEADER_VALUE

        return response 
Example #17
Source File: web_srv_custom_monitor.py    From aiomonitor with Apache License 2.0 5 votes vote down vote up
def hello(request):
    resp = web.StreamResponse()
    name = request.match_info.get('name', 'Anonymous')
    answer = ('Hello, ' + name).encode('utf8')
    resp.content_length = len(answer)
    resp.content_type = 'text/plain'
    await resp.prepare(request)
    await asyncio.sleep(10, loop=loop)
    await resp.write(answer)
    await resp.write_eof()
    return resp 
Example #18
Source File: web_srv.py    From aiomonitor with Apache License 2.0 5 votes vote down vote up
def hello(request):
    resp = web.StreamResponse()
    name = request.match_info.get('name', 'Anonymous')
    answer = ('Hello, ' + name).encode('utf8')
    resp.content_length = len(answer)
    resp.content_type = 'text/plain'
    await resp.prepare(request)
    await asyncio.sleep(100, loop=loop)
    resp.write(answer)
    await resp.write_eof()
    return resp 
Example #19
Source File: decorators.py    From aiohttp-login with ISC License 5 votes vote down vote up
def restricted_api(handler):
    @user_to_request
    @wraps(handler)
    async def decorator(*args):
        request = _get_request(args)
        if not request[cfg.REQUEST_USER_KEY]:
            return json_response({'error': 'Access denied'}, status=403)
        response = await handler(*args)
        if not isinstance(response, StreamResponse):
            response = json_response(response, dumps=json.dumps)
        return response
    return decorator 
Example #20
Source File: factories.py    From mblog with MIT License 5 votes vote down vote up
def response_factory(app, handler):
    async def response(request):
        logging.info('Response handler...')
        r = await handler(request)
        if isinstance(r, web.StreamResponse):
            return r
        if isinstance(r, bytes):
            resp = web.Response(body=r)
            resp.content_type = 'application/octet-stream'
            return resp
        if isinstance(r, str):
            if r.startswith('redirect:'):
                return web.HTTPFound(r[9:])
            resp = web.Response(body=r.encode('utf-8'))
            resp.content_type = 'text/html;charset=utf-8'
            return resp
        if isinstance(r, dict):
            template = r.get('__template__')
            if template is None:
                resp = web.Response(body=json.dumps(r, ensure_ascii=False, default=lambda o: o.__dict__).encode('utf-8'))
                resp.content_type = 'application/json;charset=utf-8'
                return resp
            else:
                # 如果用jinja2渲染,绑定已验证过的用户
                r['__user__'] = request.__user__
                resp = web.Response(body=app['__templating__'].get_template(template).render(**r).encode('utf-8'))
                resp.content_type = 'text/html;charset=utf-8'
                return resp
        if isinstance(r, int) and 100 <= r < 600:
            return web.Response(status=r)
        if isinstance(r, tuple) and len(r) == 2:
            status, message = r
            if isinstance(status, int) and 100 <= status < 600:
                return web.Response(status=status, text=str(message))
        # default
        resp = web.Response(body=str(r).encode('utf-8'))
        resp.content_type = 'text/plain;charset=utf-8'
        return resp
    return response 
Example #21
Source File: 01-error-middleware.py    From us-pycon-2019-tutorial with Apache License 2.0 5 votes vote down vote up
def error_middleware(
    request: web.Request,
    handler: Callable[[web.Request], Awaitable[web.StreamResponse]],
) -> web.StreamResponse:
    try:
        return await handler(request)
    except web.HTTPException:
        raise
    except asyncio.CancelledError:
        raise
    except Exception as ex:
        return aiohttp_jinja2.render_template(
            "error-page.html", request, {"error_text": str(ex)}, status=400
        ) 
Example #22
Source File: 01-login-session.py    From us-pycon-2019-tutorial with Apache License 2.0 5 votes vote down vote up
def check_login(request: web.Request, handler: _WebHandler) -> web.StreamResponse:
    require_login = getattr(handler, "__require_login__", False)
    session = await aiohttp_session.get_session(request)
    username = session.get("username")
    if require_login:
        if not username:
            raise web.HTTPSeeOther(location="/login")
    return await handler(request) 
Example #23
Source File: 01-login-session.py    From us-pycon-2019-tutorial with Apache License 2.0 5 votes vote down vote up
def error_middleware(
    request: web.Request, handler: _WebHandler
) -> web.StreamResponse:
    try:
        return await handler(request)
    except web.HTTPException:
        raise
    except asyncio.CancelledError:
        raise
    except Exception as ex:
        return aiohttp_jinja2.render_template(
            "error-page.html", request, {"error_text": str(ex)}, status=400
        ) 
Example #24
Source File: server.py    From us-pycon-2019-tutorial with Apache License 2.0 5 votes vote down vote up
def check_login(request: web.Request, handler: _WebHandler) -> web.StreamResponse:
    require_login = getattr(handler, "__require_login__", False)
    session = await aiohttp_session.get_session(request)
    username = session.get("username")
    if require_login:
        if not username:
            raise web.HTTPSeeOther(location="/login")
    return await handler(request) 
Example #25
Source File: server.py    From us-pycon-2019-tutorial with Apache License 2.0 5 votes vote down vote up
def error_middleware(
    request: web.Request, handler: _WebHandler
) -> web.StreamResponse:
    try:
        return await handler(request)
    except web.HTTPException:
        raise
    except asyncio.CancelledError:
        raise
    except Exception as ex:
        return aiohttp_jinja2.render_template(
            "error-page.html", request, {"error_text": str(ex)}, status=400
        ) 
Example #26
Source File: test_transport.py    From sockjs with Apache License 2.0 5 votes vote down vote up
def test_handle_session_interrupted(make_transport, make_fut):
    trans = make_transport()
    trans.session.interrupted = True
    trans.send = make_fut(1)
    trans.response = web.StreamResponse()
    await trans.handle_session()
    trans.send.assert_called_with('c[1002,"Connection interrupted"]') 
Example #27
Source File: headnode.py    From hsds with Apache License 2.0 5 votes vote down vote up
def nodestate(request):
    """HTTP method to return information about registed nodes"""
    log.request(request)
    node_type = request.match_info.get('nodetype', '*')
    node_number = '*'
    if node_type != '*':
        node_number = request.match_info.get('nodenumber', '*')

    log.info("nodestate/{}/{}".format(node_type, node_number))
    if node_type not in ("sn", "dn", "*"):
        msg="invalid node_type"
        log.response(request, code=400, message=msg)
        raise HTTPBadRequest(reason=msg)

    app = request.app
    resp = StreamResponse()
    resp.headers['Content-Type'] = 'application/json'

    if node_number == '*':
        nodes = []
        for node in app["nodes"]:
            if node["node_type"] == node_type or node_type == "*":
                nodes.append(node)
                log.debug(f"Added a node in nodestate method, up to {len(nodes)} nodes.")
        answer = {"nodes": nodes }
    else:
         answer = {}
         for node in app["nodes"]:
            if node["node_type"] == node_type and str(node["node_number"]) == node_number:
                answer = node
                break
    answer["cluster_state"] = app["cluster_state"]
    resp = json_response(answer)
    log.response(request, resp=resp)
    return resp 
Example #28
Source File: client_proxy.py    From maubot with GNU Affero General Public License v3.0 5 votes vote down vote up
def proxy(request: web.Request) -> web.StreamResponse:
    user_id = request.match_info.get("id", None)
    client = Client.get(user_id, None)
    if not client:
        return resp.client_not_found

    path = request.match_info.get("path", None)
    query = request.query.copy()
    try:
        del query["access_token"]
    except KeyError:
        pass
    headers = request.headers.copy()
    del headers["Host"]
    headers["Authorization"] = f"Bearer {client.access_token}"
    if "X-Forwarded-For" not in headers:
        peer = request.transport.get_extra_info("peername")
        if peer is not None:
            host, port = peer
            headers["X-Forwarded-For"] = f"{host}:{port}"

    data = await request.read()
    async with http.request(request.method, f"{client.homeserver}/{path}", headers=headers,
                            params=query, data=data) as proxy_resp:
        response = web.StreamResponse(status=proxy_resp.status, headers=proxy_resp.headers)
        await response.prepare(request)
        async for chunk in proxy_resp.content.iter_chunked(PROXY_CHUNK_SIZE):
            await response.write(chunk)
        await response.write_eof()
        return response 
Example #29
Source File: plugin_server.py    From maubot with GNU Affero General Public License v3.0 5 votes vote down vote up
def handle(self, request: web.Request) -> web.StreamResponse:
        match_info = await self.resolve(request)
        match_info.freeze()
        resp = None
        request._match_info = match_info
        expect = request.headers.get(hdrs.EXPECT)
        if expect:
            resp = await match_info.expect_handler(request)
            await request.writer.drain()
        if resp is None:
            handler = match_info.handler
            for middleware in self._middleware:
                handler = partial(middleware, handler=handler)
            resp = await handler(request)
        return resp 
Example #30
Source File: server.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def on_prepare(request: web.Request, response: web.StreamResponse) -> None:
    response.headers['Server'] = 'BackendAI'