本文整理汇总了Python中aiohttp.ClientResponseError方法的典型用法代码示例。如果您正苦于以下问题:Python aiohttp.ClientResponseError方法的具体用法?Python aiohttp.ClientResponseError怎么用?Python aiohttp.ClientResponseError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp
的用法示例。
在下文中一共展示了aiohttp.ClientResponseError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_text_from_url
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def get_text_from_url(self, url: str) -> str:
"""Fetch content at **url** and return as text
- On non-permanent errors (429, 502, 503, 504), the GET is retried 10 times with
increasing wait times according to fibonacci series.
- Permanent errors raise a ClientResponseError
"""
if self.cache and url in self.cache["url_text"]:
return self.cache["url_text"][url]
async with self.session.get(url) as resp:
resp.raise_for_status()
res = await resp.text()
if self.cache:
self.cache["url_text"][url] = res
return res
示例2: refresh_token
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def refresh_token(self, refresh_token):
"""
:param refresh_token: an openid refresh-token from a previous token request
"""
async with self._client_session() as client:
well_known = await self._get_well_known(client)
try:
return await self._post(
client,
well_known['token_endpoint'],
data={
'grant_type': GRANT_TYPE_REFRESH_TOKEN,
'refresh_token': refresh_token,
}
)
except aiohttp.ClientResponseError:
raise ConfigException('oidc: failed to refresh access token')
示例3: test_single_proxy
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def test_single_proxy(self, proxy):
"""
text one proxy, if valid, put them to usable_proxies.
"""
try:
async with aiohttp.ClientSession() as session:
try:
if isinstance(proxy, bytes):
proxy = proxy.decode('utf-8')
real_proxy = 'http://' + proxy
print('Testing', proxy)
async with session.get(self.test_api, proxy=real_proxy, timeout=get_proxy_timeout) as response:
if response.status == 200:
self._conn.put(proxy)
print('Valid proxy', proxy)
except (ProxyConnectionError, TimeoutError, ValueError):
print('Invalid proxy', proxy)
except (ServerDisconnectedError, ClientResponseError,ClientConnectorError) as s:
print(s)
pass
示例4: _raise_response_exceptions
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def _raise_response_exceptions(response):
try:
response.raise_for_status()
except ClientResponseError as err:
if err.status == 422:
raise AugustApiAIOHTTPError(
"The operation failed because the bridge (connect) is offline.",
) from err
if err.status == 423:
raise AugustApiAIOHTTPError(
"The operation failed because the bridge (connect) is in use.",
) from err
if err.status == 408:
raise AugustApiAIOHTTPError(
"The operation timed out because the bridge (connect) failed to respond.",
) from err
raise err
示例5: get_picture_urls
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def get_picture_urls(dates, verbose=False):
semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
tasks = [get_picture_url(date, semaphore) for date in dates]
urls = []
count = 0
# get results as jobs are done
for job in asyncio.as_completed(tasks, timeout=GLOBAL_TIMEOUT):
try:
url = yield from job
except NoPictureForDate as exc:
if verbose:
print('*** {!r} ***'.format(exc))
continue
except aiohttp.ClientResponseError as exc:
print('****** {!r} ******'.format(exc))
continue
count += 1
if verbose:
print(format(count, '3d'), end=' ')
print(url.split('/')[-1])
else:
print(url)
urls.append(url)
return urls
示例6: add_safe
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def add_safe(self, name, url, author_id):
"""Try to add an emote. Returns a string that should be sent to the user."""
if not re.fullmatch(r'\w{2,32}', name, re.ASCII):
return _(
'{name} is not a valid emote name; use 2–32 English letters, numbers and underscores.'
).format(name=discord.utils.escape_mentions(name))
try:
emote = await self.add_from_url(name, url, author_id)
except discord.HTTPException as ex:
return (
_('An error occurred while creating the emote:\n')
+ utils.format_http_exception(ex))
except ValueError:
return _('Error: Invalid URL.')
except aiohttp.ServerDisconnectedError:
return _('Error: The connection was closed early by the remote host.')
except aiohttp.ClientResponseError as exc:
raise errors.HTTPException(exc.status)
else:
return _('Emote {emote} successfully created.').format(emote=emote)
示例7: _get
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def _get(self, url, data=None, headers=None, method='GET'):
page = ''
try:
timeout = aiohttp.ClientTimeout(total=self._timeout)
async with self._sem_provider, self._session.request(
method, url, data=data, headers=headers, timeout=timeout
) as resp:
page = await resp.text()
if resp.status != 200:
log.debug(
'url: %s\nheaders: %s\ncookies: %s\npage:\n%s'
% (url, resp.headers, resp.cookies, page)
)
raise BadStatusError('Status: %s' % resp.status)
except (
UnicodeDecodeError,
BadStatusError,
asyncio.TimeoutError,
aiohttp.ClientOSError,
aiohttp.ClientResponseError,
aiohttp.ServerDisconnectedError,
) as e:
page = ''
log.debug('%s is failed. Error: %r;' % (url, e))
return page
示例8: fetch
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def fetch(self, session, url):
"""Fetch URL.
:param session: aiohttp.ClientSession
:param url: URL
:return: Response in JSON
"""
print(url)
try:
with async_timeout.timeout(Settings.timeout):
async with session.get(url) as response:
return await response.json()
except asyncio.TimeoutError:
return None
except aiohttp.ClientResponseError:
return None
示例9: __init__
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def __init__(self, exception):
if isinstance(exception, HTTPError):
super().__init__(
"Error by connection with Instagram to '%s' with response code '%s'" % (
exception.request.url,
exception.response.status_code,
),
)
self.request = exception.request
self.response = exception.response
elif isinstance(exception, ClientResponseError):
super().__init__(
"Error by connection with Instagram to '%s' with response code '%s'" % (
exception.request_info.real_url,
exception.status,
),
)
示例10: hastebin
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def hastebin(self, ctx, *, message):
"""Upload text to hastebin"""
haste_url = os.environ.get("HASTE_URL", "https://hasteb.in")
try:
async with self.bot.session.post(
haste_url + "/documents", data=message
) as resp:
key = (await resp.json())["key"]
embed = Embed(
title="Your uploaded file",
color=self.bot.main_color,
description=f"{haste_url}/" + key,
)
except (JSONDecodeError, ClientResponseError, IndexError):
embed = Embed(
color=self.bot.main_color,
description="Something went wrong. "
"We're unable to upload your text to hastebin.",
)
embed.set_footer(text="Hastebin Plugin")
await ctx.send(embed=embed)
示例11: request
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def request(self, method, url, data, headers, **kwargs):
try:
# print(url)
with async_timeout.timeout(TIMEOUT):
async with self._session.request(
method, url, headers=headers, data=data
) as response:
# print(response)
if response.status == 200 or response.status == 202:
return await response.json(loads=json_loads)
else:
raise ClientResponseError(
response.request_info,
response.history,
status=response.status,
message=response.reason,
)
except TimeoutError:
raise TimeoutError("Timeout error")
except Exception:
raise
示例12: download
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def download(self, session, update):
if self.path is None:
return
if update and not await self.needsUpdate():
return
self.path.parent.mkdir(parents=True, exist_ok=True)
async with session.get(self.url) as response:
try:
response.raise_for_status()
except aiohttp.ClientResponseError as e:
# I don't like returning Exceptions, but I can't find a better way to pass a single error in an async loop
return (self, e)
with open(self.path, "wb") as out_file:
while True:
chunk = await response.content.read(8192)
if not chunk:
break
out_file.write(chunk)
url_timestamp = getTimestamp(self.lastModified)
os.utime(self.path, (url_timestamp, url_timestamp))
return (self, None)
示例13: read_crd
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def read_crd(
*,
resource: resources.Resource,
default: Union[_T, _UNSET] = _UNSET.token,
context: Optional[auth.APIContext] = None, # injected by the decorator
) -> Union[bodies.RawBody, _T]:
if context is None:
raise RuntimeError("API instance is not injected by the decorator.")
try:
response = await context.session.get(
url=CRD_CRD.get_url(server=context.server, name=resource.name),
)
response.raise_for_status()
respdata = await response.json()
return cast(bodies.RawBody, respdata)
except aiohttp.ClientResponseError as e:
if e.status in [403, 404] and not isinstance(default, _UNSET):
return default
raise
示例14: _parse
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def _parse(response):
try:
data = await response.json()
if data is None:
raise ValueError()
except (ValueError, json.JSONDecodeError, aiohttp.ClientResponseError):
text = await response.text()
raise exception.BadHTTPResponse(response.status, text, response)
if data['ok']:
return data['result']
else:
description, error_code = data['description'], data['error_code']
# Look for specific error ...
for e in exception.TelegramError.__subclasses__():
n = len(e.DESCRIPTION_PATTERNS)
if any(map(re.search, e.DESCRIPTION_PATTERNS, n*[description], n*[re.IGNORECASE])):
raise e(description, error_code, data)
# ... or raise generic error
raise exception.TelegramError(description, error_code, data)
示例15: _check_url_async
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientResponseError [as 别名]
def _check_url_async(url: str, session: ClientSession) -> UrlResult:
"""
Connect to URL and return response status.
Parameters
----------
url : str
URL to check
session : ClientSession
aiohttp client session
Returns
-------
UrlResult
Tuple of status code, redirect history, requested url,
status/error message.
"""
try:
async with session.get(url) as resp:
try:
await resp.read()
if resp.history:
result = UrlResult(
resp.status,
resp.history,
url,
"No error. Redirect to " + str(resp.url),
)
elif resp.status == 200:
result = UrlResult(
resp.status, resp.history, url, "No error. No redirect."
)
else:
result = UrlResult(resp.status, resp.history, url, "Error?")
except ClientResponseError as client_err:
return UrlResult(client_err.status, [], url, client_err)
except ClientConnectionError as err:
result = UrlResult(404, [], url, err)
return result