aiohttp.client.request - python examples

Here are the examples of the python api aiohttp.client.request taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

0 View Complete Implementation : test_rproxy.py
Copyright MIT License
Author : intel
    async def handler_proxy(self, req):
        headers = req.headers.copy()
        self.logger.debug("headers: %s", headers)

        subdomain = self.subdomain(headers)
        path = req.path
        self.logger.debug("subdomain: %r, path: %r", subdomain, path)

        upstream = self.get_upstream(subdomain, path)
        if not upstream:
            return web.Response(status=HTTPStatus.NOT_FOUND)

        if (
            headers.get("connection", "").lower() == "upgrade"
            and headers.get("upgrade", "").lower() == "websocket"
            and req.method == "GET"
        ):
            # Handle websocket proxy
            try:
                async with aiohttp.ClientSession(
                    cookies=req.cookies
                ) as client_session:
                    async with client_session.ws_connect(
                        upstream
                    ) as ws_client:
                        ws_server = web.WebSocketResponse()
                        await ws_server.prepare(req)
                        self.loop.create_task(
                            asyncio.wait(
                                [
                                    self.wsforward(ws_server, ws_client),
                                    self.wsforward(ws_client, ws_server),
                                ],
                                return_when=asyncio.FIRST_COMPLETED,
                            )
                        )
                        return ws_server
            except aiohttp.client_exceptions.WSServerHandshakeError:
                return web.Response(status=HTTPStatus.NOT_FOUND)
        else:
            # Handle regular HTTP request proxy
            self.logger.debug(
                "upstream for (%r): %s", upstream, (subdomain, path)
            )
            async with client.request(
                req.method,
                upstream,
                headers=headers,
                allow_redirects=False,
                data=await req.read(),
            ) as res:
                self.logger.debug(
                    "upstream url(%s) status: %d", upstream, res.status
                )
                return web.Response(
                    headers=res.headers,
                    status=res.status,
                    body=await res.read(),
                )
            return ws_server