Python aiohttp.web() Examples
The following are 30
code examples of aiohttp.web().
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
, or try the search function
.
Example #1
Source File: test_runserver_main.py From aiohttp-devtools with MIT License | 6 votes |
def test_start_main_app_app_instance(tmpworkdir, loop, mocker): mktree(tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') app = web.Application() app.router.add_get('/', hello) """ }) mock_modify_main_app = mocker.patch('aiohttp_devtools.runserver.serve.modify_main_app') config = Config(app_path='app.py') await start_main_app(config, config.import_app_factory(), loop) mock_modify_main_app.assert_called_with(mock.ANY, config)
Example #2
Source File: service.py From ooi3 with GNU Affero General Public License v3.0 | 6 votes |
def get_osapi(self, request): """获取用户的内嵌游戏网页地址,返回一个JSON格式的字典。 结果中`status`键值为1时获取成功,`osapi_url`键值为内嵌网页地址;`status`为0时获取失败,`message`键值提供了错误信息。 :param request: aiohttp.web.Request :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest """ data = yield from request.post() login_id = data.get('login_id', None) password = data.get('password', None) if login_id and password: headers = aiohttp.MultiDict({'Content-Type': 'application/json'}) kancolle = KancolleAuth(login_id, password) try: osapi_url = yield from kancolle.get_osapi() result = {'status': 1, 'osapi_url': osapi_url} except OOIAuthException as e: result = {'status': 0, 'message': e.message} return aiohttp.web.Response(body=json.dumps(result).encode(), headers=headers) else: return aiohttp.web.HTTPBadRequest()
Example #3
Source File: test_runserver_main.py From aiohttp-devtools with MIT License | 6 votes |
def test_start_runserver_app_instance(tmpworkdir, loop): mktree(tmpworkdir, { 'app.py': """\ from aiohttp import web async def hello(request): return web.Response(text='<h1>hello world</h1>', content_type='text/html') app = web.Application() app.router.add_get('/', hello) """ }) aux_app, aux_port, _, _ = runserver(app_path='app.py', host='foobar.com') assert isinstance(aux_app, aiohttp.web.Application) assert aux_port == 8001 assert len(aux_app.on_startup) == 2 assert len(aux_app.on_shutdown) == 2
Example #4
Source File: eventserver.py From lrrbot with Apache License 2.0 | 6 votes |
def main(loop): global server, srv, app, handler try: os.unlink(config['eventsocket']) except FileNotFoundError: pass server = Server() await server.start(config['eventsocket'], config['event_port']) app = aiohttp.web.Application() app.router.add_route('GET', '/api/v2/events', server.negotiate) app.router.add_route('OPTIONS', '/api/v2/events', server.cors_preflight) app.on_shutdown.append(server.on_shutdown) handler = app.make_handler() srv = await loop.create_server(handler, 'localhost', 8080) if sys.platform == "win32": # On Windows Ctrl+C doesn't interrupt `select()`. def windows_is_butts(): asyncio.get_event_loop().call_later(5, windows_is_butts) windows_is_butts()
Example #5
Source File: test_streaming.py From lbry-sdk with MIT License | 6 votes |
def test_range_requests_with_blob_lru_cache(self): self.data = b'hi' self.daemon.conf.save_blobs = False self.daemon.conf.save_files = False await self.stream_create('foo', '0.01', data=self.data, file_size=0) await (await self.daemon.jsonrpc_file_list())['items'][0].fully_reflected.wait() await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo') self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir))) await self.daemon.streaming_runner.setup() site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host, self.daemon.conf.streaming_port) await site.start() self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0) await self._request_stream() self.assertItemCount(await self.daemon.jsonrpc_file_list(), 1) self.server.stop_server() # running with cache size 0 gets through without errors without # this since the server doesn't stop immediately await asyncio.sleep(1, loop=self.loop) await self._request_stream()
Example #6
Source File: bridge.py From xmppwb with MIT License | 6 votes |
def handle_incoming_webhook(self, request): """This coroutine handles incoming webhooks: It receives incoming webhooks and relays the messages to XMPP.""" if request.content_type == 'application/json': payload = await request.json() # print(payload) else: # TODO: Handle other content types payload = await request.post() # Disgard empty messages if payload['text'] == "": return aiohttp.web.Response() token = payload['token'] logging.debug("--> Handling incoming request from token " "'{}'...".format(token)) username = payload['user_name'] msg = payload['text'] for bridge in self.bridges: bridge.handle_incoming_webhook(token, username, msg) return aiohttp.web.Response()
Example #7
Source File: test_streaming.py From lbry-sdk with MIT License | 6 votes |
def _setup_stream(self, data: bytes, save_blobs: bool = True, save_files: bool = False, file_size=0): self.daemon.conf.save_blobs = save_blobs self.daemon.conf.save_files = save_files self.data = data await self.stream_create('foo', '0.01', data=self.data, file_size=file_size) if save_blobs: self.assertGreater(len(os.listdir(self.daemon.blob_manager.blob_dir)), 1) await (await self.daemon.jsonrpc_file_list())['items'][0].fully_reflected.wait() await self.daemon.jsonrpc_file_delete(delete_from_download_dir=True, claim_name='foo') self.assertEqual(0, len(os.listdir(self.daemon.blob_manager.blob_dir))) # await self._restart_stream_manager() await self.daemon.streaming_runner.setup() site = aiohttp.web.TCPSite(self.daemon.streaming_runner, self.daemon.conf.streaming_host, self.daemon.conf.streaming_port) await site.start() self.assertItemCount(await self.daemon.jsonrpc_file_list(), 0)
Example #8
Source File: server.py From snare with GNU General Public License v3.0 | 6 votes |
def start(self): app = web.Application() app.add_routes([web.route('*', '/{tail:.*}', self.handle_request)]) aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(self.dir) ) middleware = SnareMiddleware( error_404=self.meta['/status_404'].get('hash'), headers=self.meta['/status_404'].get('headers', []), server_header=self.run_args.server_header ) middleware.setup_middlewares(app) self.runner = web.AppRunner(app) await self.runner.setup() site = web.TCPSite( self.runner, self.run_args.host_ip, self.run_args.port) await site.start() names = sorted(str(s.name) for s in self.runner.sites) print("======== Running on {} ========\n" "(Press CTRL+C to quit)".format(', '.join(names)))
Example #9
Source File: service.py From ooi3 with GNU Affero General Public License v3.0 | 6 votes |
def get_flash(self, request): """获取用户的游戏FLASH地址,返回一个JSON格式的字典。 结果中`status`键值为1时获取成功,`flash_url`键值为游戏FLASH地址;`status`为0时获取失败,`message`键值提供了错误信息。 :param request: aiohttp.web.Request :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest """ data = yield from request.post() login_id = data.get('login_id', None) password = data.get('password', None) if login_id and password: headers = aiohttp.MultiDict({'Content-Type': 'application/json'}) kancolle = KancolleAuth(login_id, password) try: entry_url = yield from kancolle.get_entry() result = {'status': 1, 'flash_url': entry_url} except OOIAuthException as e: result = {'status': 0, 'message': e.message} return aiohttp.web.Response(body=json.dumps(result).encode(), headers=headers) else: return aiohttp.web.HTTPBadRequest()
Example #10
Source File: __init__.py From Flask-aiohttp with MIT License | 6 votes |
def create_aiohttp_app(self, app: flask.Flask) -> aiohttp.web.Application: """Create aiohttp web application from Flask application :param app: Flask application :returns: aiohttp web application """ # aiohttp web application instance aio_app = aiohttp.web.Application() # WSGI handler for aiohttp wsgi_handler = self.handler_factory(app) # aiohttp's router should accept any possible HTTP method of request. aio_app.router.add_route('*', r'/{path:.*}', wsgi_handler) return aio_app
Example #11
Source File: handler.py From Flask-aiohttp with MIT License | 5 votes |
def handle_request(self, request: aiohttp.web.Request): pass
Example #12
Source File: api.py From ooi3 with GNU Affero General Public License v3.0 | 5 votes |
def world_image(self, request): """ 显示正确的镇守府图片。 舰娘游戏中客户端FLASH请求的镇守府图片是根据FLASH本身的URL生成的,需要根据用户所在的镇守府IP为其显示正确的图片。 :param request: aiohttp.web.Request :return: aiohttp.web.HTTPFound or aiohttp.web.HTTPBadRequest """ size = request.match_info['size'] session = yield from get_session(request) world_ip = session['world_ip'] if world_ip: ip_sections = map(int, world_ip.split('.')) image_name = '_'.join([format(x, '03') for x in ip_sections]) + '_' + size if image_name in self.worlds: body = self.worlds[image_name] else: url = 'http://203.104.209.102/kcs/resources/image/world/' + image_name + '.png' coro = aiohttp.get(url, connector=self.connector) try: response = yield from asyncio.wait_for(coro, timeout=5) except asyncio.TimeoutError: return aiohttp.web.HTTPBadRequest() body = yield from response.read() self.worlds[image_name] = body return aiohttp.web.Response(body=body, headers={'Content-Type': 'image/png', 'Cache-Control': 'no-cache'}) else: return aiohttp.web.HTTPBadRequest()
Example #13
Source File: api.py From ooi3 with GNU Affero General Public License v3.0 | 5 votes |
def api(self, request): """ 转发客户端和游戏服务器之间的API通信。 :param request: aiohttp.web.Request :return: aiohttp.web.Response or aiohttp.web.HTTPBadRequest """ action = request.match_info['action'] session = yield from get_session(request) world_ip = session['world_ip'] if world_ip: if action == 'api_start2' and self.api_start2 is not None: return aiohttp.web.Response(body=self.api_start2, headers=aiohttp.MultiDict({'Content-Type': 'text/plain'})) else: referrer = request.headers.get('REFERER') referrer = referrer.replace(request.host, world_ip) referrer = referrer.replace('https://', 'http://') url = 'http://' + world_ip + '/kcsapi/' + action headers = aiohttp.MultiDict({ 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Origin': 'http://' + world_ip + '/', 'Referer': referrer, }) data = yield from request.post() coro = aiohttp.post(url, data=data, headers=headers, connector=self.connector) try: response = yield from asyncio.wait_for(coro, timeout=5) except asyncio.TimeoutError: return aiohttp.web.HTTPBadRequest() body = yield from response.read() if action == 'api_start2' and len(body) > 100000: self.api_start2 = body return aiohttp.web.Response(body=body, headers=aiohttp.MultiDict({'Content-Type': 'text/plain'})) else: return aiohttp.web.HTTPBadRequest()
Example #14
Source File: util.py From Flask-aiohttp with MIT License | 5 votes |
def is_websocket_request(request: aiohttp.web.Request) -> bool: """Is the request websocket request? :param request: aiohttp web request object """ upgrade = request.headers.get(hdrs.UPGRADE, '').lower().strip() connection = request.headers.get(hdrs.CONNECTION, '').lower() return 'websocket' == upgrade and 'upgrade' in connection
Example #15
Source File: __init__.py From Flask-aiohttp with MIT License | 5 votes |
def ws(self) -> aiohttp.web.WebSocketResponse: """Websocket response of aiohttp""" ws = request.environ.get('wsgi.websocket', None) if ws is None: raise RuntimeError('Request context is not a WebSocket context.') return ws
Example #16
Source File: server.py From web-boardimage with GNU Affero General Public License v3.0 | 5 votes |
def render_svg(self, request): return aiohttp.web.Response(text=self.make_svg(request), content_type="image/svg+xml")
Example #17
Source File: handler.py From Flask-aiohttp with MIT License | 5 votes |
def __call__(self, request: aiohttp.web.Request): response = yield from self.handle_request(request) return response
Example #18
Source File: decorators.py From dvhb-hybrid with MIT License | 5 votes |
def recaptcha(arg): """ Decorator to verify recaptcha response. Required properties in "recaptcha" config section: url - url to send request. active - true if recaptcha verification is active. secret - code to access api. """ def with_arg(view): @functools.wraps(view) async def wrapper(**kwargs): request = kwargs['request'] c = request.app.config.recaptcha response = None if RECAPTCHA_FIELD in request: response = request[RECAPTCHA_FIELD] else: for v in kwargs.values(): if isinstance(v, dict) and RECAPTCHA_FIELD in v: response = v.pop(RECAPTCHA_FIELD) break if not c.active: return await view(**kwargs) if response: async with aiohttp.ClientSession() as client: async with client.post(c.url, data={'secret': c.secret, 'response': response}) as r: if r.status == aiohttp.web.HTTPOk.status_code: data = await r.json() if data['success']: return await view(**kwargs) raise HTTPNotAcceptable(reason='Wrong recaptcha') return wrapper if not callable(arg): return with_arg return with_arg(arg)
Example #19
Source File: test_runserver_main.py From aiohttp-devtools with MIT License | 5 votes |
def test_run_app_aiohttp_client(tmpworkdir, aiohttp_client): mktree(tmpworkdir, SIMPLE_APP) config = Config(app_path='app.py') app_factory = config.import_app_factory() app = app_factory() modify_main_app(app, config) assert isinstance(app, aiohttp.web.Application) cli = await aiohttp_client(app) r = await cli.get('/') assert r.status == 200 text = await r.text() assert text == 'hello world'
Example #20
Source File: rapu.py From karapace with Apache License 2.0 | 5 votes |
def __init__(self, *, app_name, sentry_config): self.app_name = app_name self.app_request_metric = "{}_request".format(app_name) self.app = aiohttp.web.Application() self.app.on_startup.append(self.create_http_client) self.app.on_cleanup.append(self.cleanup_http_client) self.http_client_v = None self.http_client_no_v = None self.log = logging.getLogger(self.app_name) self.stats = StatsClient(sentry_config=sentry_config) self.raven_client = self.stats.raven_client self.app.on_cleanup.append(self.cleanup_stats_client)
Example #21
Source File: rapu.py From karapace with Apache License 2.0 | 5 votes |
def run(self, *, host, port): aiohttp.web.run_app( app=self.app, host=host, port=port, access_log_format='%Tfs %{x-client-ip}i "%r" %s "%{user-agent}i" response=%bb request_body=%{content-length}ib', )
Example #22
Source File: test_website.py From website with MIT License | 5 votes |
def test_endpoint_slack(client): r = await client.get("/web/slack") assert r.status == 200
Example #23
Source File: mock_server.py From aiobotocore with Apache License 2.0 | 5 votes |
def ok(request): return aiohttp.web.Response()
Example #24
Source File: eventserver.py From lrrbot with Apache License 2.0 | 5 votes |
def get_last_events(self, request): try: last_event_id = int(request.headers.get('Last-Event-Id', request.query.get('last-event-id'))) except (ValueError, TypeError): last_event_id = None interval = request.query.get('interval') if interval is not None and last_event_id is None: last_event_id = 0 if last_event_id is not None: events = self.metadata.tables['events'] query = sqlalchemy.select([ events.c.id, events.c.event, events.c.data, events.c.time ]) query = query.where(events.c.id > last_event_id) if interval is not None: query = query.where(events.c.time > sqlalchemy.func.current_timestamp() - sqlalchemy.cast(interval, sqlalchemy.Interval)) query = query.order_by(events.c.id) try: with self.engine.begin() as conn: return [ {'id': id, 'event': event, 'data': dict(data, time=time.isoformat())} for id, event, data, time in conn.execute(query) ] except sqlalchemy.exc.DataError as e: raise aiohttp.web.HTTPBadRequest from e return []
Example #25
Source File: eventserver.py From lrrbot with Apache License 2.0 | 5 votes |
def event_stream(self, request): queue = asyncio.Queue() for event in self.get_last_events(request): await queue.put(event) self.queues.append(queue) response = aiohttp.web.StreamResponse() response.enable_chunked_encoding() response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Content-Type'] = 'text/event-stream; charset=utf-8' response.headers['Vary'] = "Accept" await response.prepare(request) while True: try: try: event = await asyncio.wait_for(queue.get(), 15) if event['event'] is Poison: break await response.write(b"id:%d\n" % event['id']) await response.write(b"event:%s\n" % event['event'].encode('utf-8')) await response.write(b"data:%s\n" % json.dumps(event['data']).encode('utf-8')) await response.write(b"\n") queue.task_done() except asyncio.TimeoutError: await response.write(b":keep-alive\n\n") except IOError: break self.queues.remove(queue) return response
Example #26
Source File: eventserver.py From lrrbot with Apache License 2.0 | 5 votes |
def json(self, request): return aiohttp.web.json_response({ 'events': self.get_last_events(request), }, headers={"Vary": "Accept", 'Access-Control-Allow-Origin': request.headers.get('Origin', '*')})
Example #27
Source File: eventserver.py From lrrbot with Apache License 2.0 | 5 votes |
def cors_preflight(self, request): return aiohttp.web.Response(headers={ 'Access-Control-Allow-Origin': request.headers.get('Origin', '*'), })
Example #28
Source File: test_api.py From nima.pytorch with MIT License | 5 votes |
def api(config: Config) -> AsyncIterator[ApiConfig]: app = await create_app(config) runner = aiohttp.web.AppRunner(app) await runner.setup() api_config = ApiConfig(host="0.0.0.0", port=8080) site = aiohttp.web.TCPSite(runner, api_config.host, api_config.port) await site.start() yield api_config await runner.cleanup()
Example #29
Source File: test_website.py From website with MIT License | 5 votes |
def test_endpoint_index(client): r = await client.get("/") assert r.history[0].url.path == "/" assert r.history[0].status == 302 assert r.status == 200 assert r.url.path == "/web"
Example #30
Source File: service.py From graphql-over-kafka with MIT License | 5 votes |
def init_app(self): from nautilus.api.endpoints import template_dir as api_template_dir from nautilus.auth import template_dir as auth_template_dir # the secret key secret_key = 'NERbTdtQl7IrBM9kx1PDjJXiyZhWWBZ9E7q2B3U7KVE=' # create a web application instance self.app = aiohttp.web.Application( middlewares=[ session_middleware( EncryptedCookieStorage(secret_key, secure=True, domain='*') ) ] ) # add the template loader aiohttp_jinja2.setup(self.app, loader=jinja2.ChoiceLoader([ jinja2.FileSystemLoader(api_template_dir), jinja2.FileSystemLoader(auth_template_dir) ]) ) # TODO: # debug mode # attach the ioloop to the application self.loop = asyncio.get_event_loop() # attach the service to the loop self.loop.service = self