本文整理汇总了Python中aiohttp.ClientResponse方法的典型用法代码示例。如果您正苦于以下问题:Python aiohttp.ClientResponse方法的具体用法?Python aiohttp.ClientResponse怎么用?Python aiohttp.ClientResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp
的用法示例。
在下文中一共展示了aiohttp.ClientResponse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _query_qna_service
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def _query_qna_service(
self, turn_context: TurnContext, options: QnAMakerOptions
) -> QueryResults:
url = f"{ self._endpoint.host }/knowledgebases/{ self._endpoint.knowledge_base_id }/generateAnswer"
question = GenerateAnswerRequestBody(
question=turn_context.activity.text,
top=options.top,
score_threshold=options.score_threshold,
strict_filters=options.strict_filters,
context=options.context,
qna_id=options.qna_id,
is_test=options.is_test,
ranker_type=options.ranker_type,
)
http_request_helper = HttpRequestUtils(self._http_client)
response: ClientResponse = await http_request_helper.execute_http_request(
url, question, self._endpoint, options.timeout
)
result: QueryResults = await self._format_qna_result(response, options)
return result
示例2: fetch
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def fetch(self, url, **kwargs) -> aiohttp.ClientResponse:
headers = {"User-Agent": get_user_agent()}
async with aiohttp.ClientSession(
conn_timeout=self.config["response_timeout"],
read_timeout=self.config["response_timeout"],
) as session:
try:
async with session.get(
urljoin(self.host, url), headers=headers, **kwargs
) as response:
await response.text()
return response
except aiohttp.ClientConnectionError:
raise exceptions.SlaveDoesNotExist(
f"Unable to connect to the slave at {self.host}"
)
示例3: _read_content
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def _read_content(self, response: aiohttp.ClientResponse):
content_length = response.headers.get('Content-Length')
if content_length:
content_length = int(content_length)
if content_length > self.max_content_length:
msg = 'content length {} larger than limit {}'.format(
content_length, self.max_content_length)
raise ContentTooLargeError(msg)
content_length = 0
content = bytearray()
async for chunk in response.content.iter_chunked(8 * 1024):
content_length += len(chunk)
if content_length > self.max_content_length:
msg = 'content length larger than limit {}'.format(
self.max_content_length)
raise ContentTooLargeError(msg)
content.extend(chunk)
return content
示例4: _read
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def _read(
self, url, etag=None, last_modified=None, referer=None,
headers=None, ignore_content=False
) -> aiohttp.ClientResponse:
headers = self._prepare_headers(
etag=etag,
last_modified=last_modified,
referer=referer,
headers=headers,
)
await self._async_init()
if not self.allow_private_address:
await self.check_private_address(url)
async with self.session.get(url, headers=headers) as response:
content = None
if not is_ok_status(response.status) or not ignore_content:
content = await self._read_content(response)
if not is_ok_status(response.status):
return response.headers, content, url, response.status
self.check_content_type(response)
return response.headers, content, str(response.url), response.status
示例5: setUp
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def setUp(self):
self.main_page_path = generate_unique_path()
os.makedirs(self.main_page_path)
self.dorks = dict(response={'dorks': "test_dorks"})
self.loop = asyncio.new_event_loop()
aiohttp.ClientSession.get = AsyncMock(
return_value=aiohttp.ClientResponse(
url=yarl.URL("http://www.example.com"),
method="GET",
writer=None,
continue100=1,
timer=None,
request_info=None,
traces=None,
loop=self.loop,
session=None))
no_dorks = True
tanner = "tanner.mushmush.org"
self.handler = HtmlHandler(no_dorks, tanner)
self.data = None
示例6: test_event_result
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def test_event_result(self):
aiohttp.ClientResponse.json = AsyncMock(
return_value=dict(
detection={
'type': 1},
sess_uuid="test_uuid"))
async def test():
self.result = await self.handler.submit_data(self.data)
self.loop.run_until_complete(test())
self.assertEqual(
self.result,
dict(
detection={
'type': 1},
sess_uuid="test_uuid"))
示例7: _wrap_response
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def _wrap_response(
self,
response: aiohttp.ClientResponse,
url: str,
**kwargs: Union[int, Optional[str]],
) -> Dict[str, Any]:
"""Parses the response as json, then runs check_response and
add_jikan_metadata
"""
json_response: Dict[str, Any] = {}
try:
json_response = await response.json()
if not isinstance(json_response, dict):
json_response = {"data": json_response}
except (json.decoder.JSONDecodeError, simplejson.JSONDecodeError):
json_response = {"error": await response.text()}
if response.status >= 400:
raise APIException(response.status, json_response, **kwargs)
return utils.add_jikan_metadata(response, json_response, url)
示例8: get
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def get(
self, path: str, required_serial: Optional[int], **kw: Any
) -> AsyncGenerator[aiohttp.ClientResponse, None]:
logger.debug(f"Getting {path} (serial {required_serial})")
if not path.startswith(("https://", "http://")):
path = self.url + path
async with self.session.get(path, **kw) as r:
got_serial = (
int(r.headers[PYPI_SERIAL_HEADER])
if PYPI_SERIAL_HEADER in r.headers
else None
)
await self.check_for_stale_cache(path, required_serial, got_serial)
yield r
# TODO: Add storage backend support / refactor - #554
示例9: validate
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def validate(self, response: aiohttp.ClientResponse, checker_result: CheckerResult) -> bool:
if response.status != 200:
return False
json_result = await response.json()
if 'ip' in json_result:
checker_result.ipv4 = json_result['ip']
if 'city' in json_result:
checker_result.city = json_result['city']
if 'region' in json_result:
checker_result.region = json_result['region']
if 'country' in json_result:
checker_result.country_code = json_result['country']
if 'loc' in json_result:
checker_result.location_coordinates = tuple(float(x) for x in json_result['loc'].split(','))
if 'org' in json_result:
checker_result.organization_name = json_result['org']
return True
示例10: test_iter_lines_generator
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def test_iter_lines_generator():
"""Test that lines are split correctly."""
async def mock_iter_content(n):
for chunk in [b'1\r\n2\r\n', b'3\r', b'\n4', b'\r\n5']:
yield chunk
response = ClientResponse(
'get', ST_URL,
request_info=Mock(),
writer=Mock(),
continue100=None,
timer=TimerNoop(),
traces=[],
loop=Mock(),
session=Mock(),
)
response._headers = {'Content-Type': 'application/json;charset=utf-8'}
with patch.object(response, 'content', Mock(iter_chunked=mock_iter_content)):
result = [
line async for line in _iter_lines_generator(
response=response, decode_unicode=True
)
]
assert result == ['1', '2', '3', '4', '5']
示例11: get
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def get(self, path, *path_args, params=None):
"""Sends a GET request to a given API endpoint.
This is a low-level function that returns a raw HTTP response, no error
checking nor response parsing is performed. See :func:`get_json`,
:func:`get_data` and :func:`get_object` for higher-level functions.
:param path: Path to API endpoint, can contain format placeholders {}.
:param path_args: A variable number of arguments that are put into any
placeholders used in path.
:param params: Parameters sent in the request.
:type path: str
:type params: dict
:returns: An instance of :class:`ClientResponse`.
"""
return _make_sync(self.get_async(path, *path_args, params=params))
示例12: get_error_async
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def get_error_async(self, response):
"""Given a :class:`ClientResponse` returns a :class:`APIError`
This function checks if the response from the VirusTotal backend was an
error and returns the appropiate :class:`APIError` or None if no error
occurred.
:param response: A :class:`ClientResponse` instance.
:returns: An instance of :class:`APIError` or None.
"""
if response.status == 200:
return None
if response.status >= 400 and response.status <= 499:
if response.content_type == 'application/json':
json_response = await response.json_async()
error = json_response.get('error')
if error:
return APIError.from_dict(error)
return APIError('ClientError', await response.text_async())
return APIError('ServerError', await response.text_async())
示例13: post
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def post(self, path, *path_args, data=None):
"""Sends a POST request to a given API endpoint.
This is a low-level function that returns a raw HTTP response, no error
checking nor response parsing is performed. See :func:`post_object` for
a higher-level function.
:param path: Path to API endpoint, can contain format placeholders {}.
:param path_args: A variable number of arguments that are put into any
placeholders used in path.
:param data: Data sent in the request body.
:type path: str
:type data: A string or bytes
:returns: An instance of :class:`ClientResponse`.
"""
return _make_sync(self.post_async(path, *path_args, data=data))
示例14: request
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def request(self, method: str, url: str,
**kwargs: Any
) -> Tuple[aiohttp.ClientResponse, Union[str, dict]]:
try:
params = kwargs['params']
if isinstance(params, dict):
kwargs['params'] = {k: (str(v).lower() if isinstance(v, bool)
else v) for k, v in params.items()}
else:
kwargs['params'] = [(k, (str(v).lower() if isinstance(v, bool)
else v)) for k, v in params]
except KeyError:
pass
pre_time = time.time()
async with self.__session.request(method, url, **kwargs) as r:
log.debug('{0} {1} has returned {2.status} in {3:.2f}s'.format(
method,
url,
r,
time.time() - pre_time
))
data = await self.json_or_text(r)
return r, data
示例15: get_snapshot
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponse [as 别名]
def get_snapshot(client: aiohttp.ClientSession, trading_pair: str, limit: int = 1000) -> Dict[str, Any]:
original_trading_pair: str = trading_pair
params: Dict[str, str] = {"count": str(limit), "pair": trading_pair} if limit != 0 else {"pair": trading_pair}
async with client.get(SNAPSHOT_REST_URL, params=params) as response:
response: aiohttp.ClientResponse = response
if response.status != 200:
raise IOError(f"Error fetching Kraken market snapshot for {original_trading_pair}. "
f"HTTP status is {response.status}.")
response_json = await response.json()
if len(response_json["error"]) > 0:
raise IOError(f"Error fetching Kraken market snapshot for {original_trading_pair}. "
f"Error is {response_json['error']}.")
data: Dict[str, Any] = next(iter(response_json["result"].values()))
data = {"trading_pair": trading_pair, **data}
data["latest_update"] = max([*map(lambda x: x[2], data["bids"] + data["asks"])], default=0.)
# Need to add the symbol into the snapshot message for the Kafka message queue.
# Because otherwise, there'd be no way for the receiver to know which market the
# snapshot belongs to.
return data