本文整理汇总了Python中aiohttp.web.StreamResponse方法的典型用法代码示例。如果您正苦于以下问题:Python web.StreamResponse方法的具体用法?Python web.StreamResponse怎么用?Python web.StreamResponse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web
的用法示例。
在下文中一共展示了web.StreamResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: info
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def info(request):
"""HTTP Method to return node state to caller"""
log.request(request)
app = request.app
resp = StreamResponse()
resp.headers['Content-Type'] = 'application/json'
answer = {}
# copy relevant entries from state dictionary to response
answer['id'] = request.app['id']
answer['start_time'] = unixTimeToUTC(app['start_time'])
answer['last_health_check'] = unixTimeToUTC(app['last_health_check'])
answer['up_time'] = elapsedTime(app['start_time'])
answer['cluster_state'] = app['cluster_state']
answer['bucket_name'] = app['bucket_name']
answer['target_sn_count'] = await getTargetNodeCount(app, "sn")
answer['active_sn_count'] = getActiveNodeCount(app, "sn")
answer['target_dn_count'] = await getTargetNodeCount(app, "dn")
answer['active_dn_count'] = getActiveNodeCount(app, "dn")
resp = json_response(answer)
log.response(request, resp=resp)
return resp
示例2: test_stream_response
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例3: process
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例4: process
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例5: __call__
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例6: __init__
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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)
示例7: nodestate
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例8: proxy
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例9: handle
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
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
示例10: on_prepare
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def on_prepare(request: web.Request, response: web.StreamResponse) -> None:
response.headers['Server'] = 'BackendAI'
示例11: api_middleware
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def api_middleware(request: web.Request,
handler: WebRequestHandler) -> web.StreamResponse:
_handler = handler
method_override = request.headers.get('X-Method-Override', None)
if method_override:
request = request.clone(method=method_override)
new_match_info = await request.app.router.resolve(request)
if new_match_info is None:
raise InternalServerError('No matching method handler found')
_handler = new_match_info.handler
request._match_info = new_match_info # type: ignore # this is a hack
ex = request.match_info.http_exception
if ex is not None:
# handled by exception_middleware
raise ex
new_api_version = request.headers.get('X-BackendAI-Version')
legacy_api_version = request.headers.get('X-Sorna-Version')
api_version = new_api_version or legacy_api_version
try:
if api_version is None:
path_major_version = int(request.match_info.get('version', 5))
revision_date = LATEST_REV_DATES[path_major_version]
request['api_version'] = (path_major_version, revision_date)
else:
assert api_version in VALID_VERSIONS
hdr_major_version, revision_date = api_version.split('.', maxsplit=1)
request['api_version'] = (int(hdr_major_version[1:]), revision_date)
except (AssertionError, ValueError, KeyError):
raise GenericBadRequest('Unsupported API major version.')
resp = (await _handler(request))
return resp
示例12: exception_middleware
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def exception_middleware(request: web.Request,
handler: WebRequestHandler) -> web.StreamResponse:
app = request.app
error_monitor = app['error_monitor']
stats_monitor = app['stats_monitor']
try:
await stats_monitor.report_metric(INCREMENT, 'ai.backend.gateway.api.requests')
resp = (await handler(request))
except BackendError as ex:
if ex.status_code == 500:
log.exception('Internal server error raised inside handlers')
raise
await error_monitor.capture_exception()
await stats_monitor.report_metric(INCREMENT, 'ai.backend.gateway.api.failures')
await stats_monitor.report_metric(INCREMENT, f'ai.backend.gateway.api.status.{ex.status_code}')
raise
except web.HTTPException as ex:
await stats_monitor.report_metric(INCREMENT, 'ai.backend.gateway.api.failures')
await stats_monitor.report_metric(INCREMENT, f'ai.backend.gateway.api.status.{ex.status_code}')
if ex.status_code == 404:
raise GenericNotFound
if ex.status_code == 405:
concrete_ex = cast(web.HTTPMethodNotAllowed, ex)
raise MethodNotAllowed(concrete_ex.method, concrete_ex.allowed_methods)
log.warning('Bad request: {0!r}', ex)
raise GenericBadRequest
except asyncio.CancelledError as e:
# The server is closing or the client has disconnected in the middle of
# request. Atomic requests are still executed to their ends.
log.debug('Request cancelled ({0} {1})', request.method, request.rel_url)
raise e
except Exception as e:
await error_monitor.capture_exception()
log.exception('Uncaught exception in HTTP request handlers {0!r}', e)
if app['config']['debug']['enabled']:
raise InternalServerError(traceback.format_exc())
else:
raise InternalServerError()
else:
await stats_monitor.report_metric(INCREMENT, f'ai.backend.gateway.api.status.{resp.status}')
return resp
示例13: rlim_middleware
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def rlim_middleware(app: web.Application,
request: web.Request,
handler: WebRequestHandler) -> web.StreamResponse:
# This is a global middleware: request.app is the root app.
now = Decimal(time.time()).quantize(_time_prec)
rr = app['redis_rlim']
if request['is_authorized']:
rate_limit = request['keypair']['rate_limit']
access_key = request['keypair']['access_key']
ret = await redis.execute_script(
rr, 'ratelimit', _rlim_script,
[access_key],
[str(now), str(_rlim_window)],
)
rolling_count = int(ret)
if rolling_count > rate_limit:
raise RateLimitExceeded
remaining = rate_limit - rolling_count
response = await handler(request)
response.headers['X-RateLimit-Limit'] = str(rate_limit)
response.headers['X-RateLimit-Remaining'] = str(remaining)
response.headers['X-RateLimit-Window'] = str(_rlim_window)
return response
else:
# No checks for rate limiting for non-authorized queries.
response = await handler(request)
response.headers['X-RateLimit-Limit'] = '1000'
response.headers['X-RateLimit-Remaining'] = '1000'
response.headers['X-RateLimit-Window'] = str(_rlim_window)
return response
示例14: get_task_logs
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def get_task_logs(request: web.Request, params: Any) -> web.StreamResponse:
log.info('GET_TASK_LOG (ak:{}, k:{})',
request['keypair']['access_key'], params['kernel_id'])
domain_name = request['user']['domain_name']
user_role = request['user']['role']
user_uuid = request['user']['uuid']
raw_kernel_id = params['kernel_id'].hex
mount_prefix = await request.app['config_server'].get('volumes/_mount')
fs_prefix = await request.app['config_server'].get('volumes/_fsprefix')
async with request.app['dbpool'].acquire() as conn, conn.begin():
matched_vfolders = await query_accessible_vfolders(
conn, user_uuid,
user_role=user_role, domain_name=domain_name,
allowed_vfolder_types=['user'],
extra_vf_conds=(vfolders.c.name == '.logs'))
if not matched_vfolders:
raise GenericNotFound('You do not have ".logs" vfolder for persistent task logs.')
log_vfolder = matched_vfolders[0]
log_path = (
Path(mount_prefix) / log_vfolder['host'] / Path(fs_prefix.lstrip('/')) /
log_vfolder['id'].hex /
'task' / raw_kernel_id[:2] / raw_kernel_id[2:4] / f'{raw_kernel_id[4:]}.log'
)
def check_file():
if not log_path.is_file():
raise GenericNotFound('The requested log file or the task was not found.')
try:
with open(log_path, 'rb'):
pass
except IOError:
raise GenericNotFound('The requested log file is not readable.')
loop = current_loop()
await loop.run_in_executor(None, check_file)
return web.FileResponse(log_path, headers={
hdrs.CONTENT_TYPE: "text/plain",
})
示例15: binary
# 需要导入模块: from aiohttp import web [as 别名]
# 或者: from aiohttp.web import StreamResponse [as 别名]
def binary(self, data, content_type='application/octet-stream', file_name=None):
self.response = web.StreamResponse()
self.response.content_length = len(data)
self.response.content_type = content_type
if file_name:
for char in '/<>:\"\'\\|?* ':
file_name = file_name.replace(char, '')
self.response.headers.add('Content-Disposition',
'attachment; filename="{}"'.format(file_name))
await self.response.prepare(self.request)
await self.response.write(data)