How to use aiohttp - 10 common examples

To help you get started, we’ve selected a few aiohttp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github instana / python-sensor / tests / frameworks / test_aiohttp.py View on Github external
async def test():
            with async_tracer.start_active_span('test'):
                async with aiohttp.ClientSession() as session:
                    return await self.fetch(session, testenv["wsgi_server"] + "/500")
github Python3WebSpider / ProxyPool / proxypool / processors / tester.py View on Github external
async def test(self, proxy: Proxy):
        """
        test single proxy
        :param proxy: Proxy object
        :return:
        """
        async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
            try:
                logger.debug(f'testing {proxy.string()}')
                # if TEST_ANONYMOUS is True, make sure that
                # the proxy has the effect of hiding the real IP
                if TEST_ANONYMOUS:
                    url = 'https://httpbin.org/ip'
                    async with session.get(url, timeout=TEST_TIMEOUT) as response:
                        resp_json = await response.json()
                        origin_ip = resp_json['origin']
                    async with session.get(url, proxy=f'http://{proxy.string()}', timeout=TEST_TIMEOUT) as response:
                        resp_json = await response.json()
                        anonymous_ip = resp_json['origin']
                    assert origin_ip != anonymous_ip
                    assert proxy.host == anonymous_ip
                async with session.get(TEST_URL, proxy=f'http://{proxy.string()}', timeout=TEST_TIMEOUT,
                                       allow_redirects=False) as response:
github armills / jsonrpc-websocket / tests.py View on Github external
def test_error(self):
        self.receive_queue.put_nowait(aiohttp.WSMessage(aiohttp.WSMsgType.ERROR, 0, ''))
github hh-h / aiohttp-swagger3 / tests / test_named_resources.py View on Github external
async def test_named_resources(swagger_docs):
    async def handler(request):
        return web.json_response()

    swagger = swagger_docs()
    swagger.add_routes(
        [web.get("/", handler, name="get"), web.post("/", handler, name="post")]
    )

    assert "get" in swagger._app.router
    assert "post" in swagger._app.router
github aio-libs / aiohttp / tests / test_client_functional2.py View on Github external
def handler(request):
            self.assertEqual(len(data), request.content_length)
            val = yield from request.read()
            self.assertEqual(data, val)
            return web.Response()
github aio-libs / aiohttp / tests / test_web_app.py View on Github external
sub = web.Application()
    root.add_subapp('/sub', sub)
    root.freeze()
    assert root._run_middlewares is False

    @web.middleware
    async def middleware(request, handler):
        return await handler(request)

    root = web.Application(middlewares=[middleware])
    sub = web.Application()
    root.add_subapp('/sub', sub)
    root.freeze()
    assert root._run_middlewares is True

    root = web.Application()
    sub = web.Application(middlewares=[middleware])
    root.add_subapp('/sub', sub)
    root.freeze()
    assert root._run_middlewares is True
github asvetlov / us-pycon-2019-tutorial / code / 10-testing / proj / server.py View on Github external
async def init_app(db_path: Path) -> web.Application:
    app = web.Application(client_max_size=64 * 1024 ** 2)
    app["DB_PATH"] = db_path
    app.add_routes(router)
    app.cleanup_ctx.append(init_db)
    aiohttp_session.setup(app, aiohttp_session.SimpleCookieStorage())
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.FileSystemLoader(str(Path(__file__).parent / "templates")),
        context_processors=[username_ctx_processor],
    )
    app.middlewares.append(error_middleware)
    app.middlewares.append(check_login)

    return app
github aio-libs / aiohttp-cors / tests / integration / test_main.py View on Github external
def inner(defaults, route_config):
        app = web.Application()
        cors = _setup(app, defaults=defaults)

        if request.param == 'resource':
            resource = cors.add(app.router.add_resource("/resource"))
            cors.add(resource.add_route("GET", handler), route_config)
        elif request.param == 'view':
            WebViewHandler.cors_config = route_config
            cors.add(
                app.router.add_route("*", "/resource", WebViewHandler))
        elif request.param == 'route':
            cors.add(
                app.router.add_route("GET", "/resource", handler),
                route_config)
        else:
            raise RuntimeError('unknown parameter {}'.format(request.param))
github foglamp / FogLAMP / tests / unit / python / foglamp / services / core / api / test_support.py View on Github external
def client(self, loop, test_client):
        app = web.Application(loop=loop)
        # fill the routes table
        routes.setup(app)
        return loop.run_until_complete(test_client(app))
github FAForever / server / tests / unit_tests / test_lobbyconnection.py View on Github external
async def token(request):
        data = await request.json()
        return web.json_response({'result': data.get('uid_hash')})