本文整理汇总了Python中aiohttp.ClientError方法的典型用法代码示例。如果您正苦于以下问题:Python aiohttp.ClientError方法的具体用法?Python aiohttp.ClientError怎么用?Python aiohttp.ClientError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp
的用法示例。
在下文中一共展示了aiohttp.ClientError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def fetch(self, url, max_redirect):
tries = 0
exception = None
while tries < self.max_tries:
try:
response = await self.session.get(
url, allow_redirects=False)
break
except aiohttp.ClientError as client_error:
exception = client_error
tries += 1
else:
return
try:
next_url = await self.parse_link(response)
print('{} has finished'.format(url))
if next_url is not None:
self.add_url(next_url, max_redirect)
finally:
response.release()
示例2: aiohttp_repeat
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def aiohttp_repeat(func=None, *, count: int = 4):
if func is None:
return partial(func, count=count)
async def wrapper(*args: Any, **kwargs: Any) -> Optional[Any]:
for pause in range(1, count + 1):
try:
return await func(*args, **kwargs)
except ClientError:
if pause == count:
raise
logger.debug('aiohttp payload error, repeating...', exc_info=True)
sleep(pause)
raise RuntimeError('unreachable')
wrapper = update_wrapper(wrapper=wrapper, wrapped=func)
return wrapper
示例3: wait_til_server_is_running
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def wait_til_server_is_running(endpoint,
max_retries=30,
sleep_between_retries=1):
"""Try to reach the server, retry a couple of times and sleep in between."""
while max_retries:
try:
r = await retrieve_status(endpoint)
logger.info("Reached core: {}".format(r))
if not r.get("is_ready"):
# server did not finish loading the agent yet
# in this case, we need to wait till the model trained
# so we might be sleeping for a while...
await asyncio.sleep(sleep_between_retries)
continue
else:
# server is ready to go
return True
except ClientError:
max_retries -= 1
if max_retries:
await asyncio.sleep(sleep_between_retries)
return False
示例4: get_response
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def get_response(session, url, headers):
try:
response = await session.get(url, headers=headers)
except (OSError, TimeoutError, IOError, aiohttp.ClientError) as ex:
await session.close()
raise ImageProxyError(str(ex))
except Exception:
await session.close()
raise
if yarl.URL(response.url) != yarl.URL(url):
try:
await check_private_address(str(response.url))
except Exception:
await session.close()
raise
return response
示例5: test_perform_request_ssl_error
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def test_perform_request_ssl_error(auto_close, loop):
for exc, expected in [
(aiohttp.ClientConnectorCertificateError(mock.Mock(), mock.Mock()), SSLError), # noqa
(aiohttp.ClientConnectorSSLError(mock.Mock(), mock.Mock()), SSLError),
(aiohttp.ClientSSLError(mock.Mock(), mock.Mock()), SSLError),
(aiohttp.ClientError('Other'), ConnectionError),
(asyncio.TimeoutError, ConnectionTimeout),
]:
session = aiohttp.ClientSession(loop=loop)
async def coro(*args, **Kwargs):
raise exc
session._request = coro
conn = auto_close(AIOHttpConnection(session=session, loop=loop,
use_ssl=True))
with pytest.raises(expected):
await conn.perform_request('HEAD', '/')
示例6: _fetch
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def _fetch(self, resource: str,) -> Dict[Any, Any]:
""" Fetch JSON data from a web or file resource and return a dict """
logger.debug(f"fetching {resource}")
if resource.startswith("http"):
try:
async with aiohttp.ClientSession() as session:
async with session.get(
resource, timeout=self.fetch_timeout
) as resp:
if not resp.status == 200:
raise Exception(f"Fetch failed {resp.status}: {resource}")
data = await resp.json()
except asyncio.TimeoutError:
raise Exception(f"Request timed out to {resource}") from None
except aiohttp.ClientError as exc:
raise Exception(f"Client error {exc}, {resource}") from None
else:
with open(resource, "rt") as f:
data = json.loads(f.read())
return data
示例7: search
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def search(self,query,page):
params = {
"Query":query,
"$skip": self.parameters["$top"] * page
}
params.update(self.parameters)
try:
r = yield from aiohttp.request(
'get',
self.url,
params=params,
headers=self.headers
)
results = yield from r.json()
yield from self.__process(results)
except aiohttp.ClientError as client_error:
print("Error: {emsg}".format(emsg=client_error))
示例8: get_highscores
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def get_highscores(world, category=Category.EXPERIENCE, vocation=VocationFilter.ALL, *, tries=5) \
-> Optional[Highscores]:
"""Gets all the highscores entries of a world, category and vocation."""
# TODO: Add caching
if tries == 0:
raise errors.NetworkError(f"get_highscores({world},{category},{vocation})")
try:
async with aiohttp.ClientSession() as session:
async with session.get(Highscores.get_url_tibiadata(world, category, vocation)) as resp:
content = await resp.text()
highscores = Highscores.from_tibiadata(content, vocation)
except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException):
await asyncio.sleep(config.network_retry_delay)
return await get_highscores(world, category, vocation, tries=tries - 1)
return highscores
示例9: get_world
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def get_world(name, *, tries=5) -> Optional[World]:
name = name.strip().title()
if tries == 0:
raise errors.NetworkError(f"get_world({name})")
try:
world = CACHE_WORLDS[name]
return world
except KeyError:
pass
try:
async with aiohttp.ClientSession() as session:
async with session.get(World.get_url_tibiadata(name)) as resp:
content = await resp.text(encoding='ISO-8859-1')
world = World.from_tibiadata(content)
except (aiohttp.ClientError, asyncio.TimeoutError, tibiapy.TibiapyException):
await asyncio.sleep(config.network_retry_delay)
return await get_world(name, tries=tries - 1)
CACHE_WORLDS[name] = world
return world
示例10: fetch
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def fetch(self, url):
"""Fetch request."""
error_msg = None
try:
async with aiohttp.ClientSession() as session:
body = await self.fetch_with_session(session, url)
except asyncio.TimeoutError:
error_msg = 'Request timed out'
raise ClashRoyaleAPIError(message=error_msg)
except aiohttp.ServerDisconnectedError as err:
error_msg = 'Server disconnected error: {}'.format(err)
raise ClashRoyaleAPIError(message=error_msg)
except (aiohttp.ClientError, ValueError) as err:
error_msg = 'Request connection error: {}'.format(err)
raise ClashRoyaleAPIError(message=error_msg)
except json.JSONDecodeError:
error_msg = "Non JSON returned"
raise ClashRoyaleAPIError(message=error_msg)
else:
return body
finally:
if error_msg is not None:
raise ClashRoyaleAPIError(message=error_msg)
示例11: get
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def get(self, url: str, params: Dict[str, str] = None) -> Response:
"""Perform HTTP GET request.
:param url: the request url
:param params: the request params
:return: the response from server
:raise: :exc:`ConnectionError <stellar_sdk.exceptions.ConnectionError>`
"""
try:
response = await self._session.get(url, params=params)
return Response(
status_code=response.status,
text=await response.text(),
headers=dict(response.headers),
url=str(response.url),
)
except aiohttp.ClientError as e: # TODO: need more research
raise ConnectionError(e)
示例12: async_update
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def async_update(self):
try:
auth = aiohttp.BasicAuth(self.username, self.password)
with async_timeout.timeout(TIMEOUT, loop=self.hass.loop):
response = await self.websession.get(ENDPOINT, auth=auth)
data = await response.json(content_type=None)
if len(data) > 0:
_LOGGER.debug("Updating sensor: {}".format(data))
entry = data[0]
self._meal = entry['meal']
self.extract_deilver_date(entry['deliveryDate'])
else:
_LOGGER.debug("No data to update: {}".format(data))
self._deliver_from = None
self._deliver_to = None
self._time_left = None
self._meal = None
except (asyncio.TimeoutError, aiohttp.ClientError, IndexError) as error:
_LOGGER.error("Failed getting devices: %s", error)
示例13: async_update
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def async_update(self):
try:
from bs4 import BeautifulSoup
with async_timeout.timeout(TIMEOUT, loop=self.hass.loop):
response = await self.websession.get(ENDPOINT, params={ "identityNumber": self.identity_id, "cityCardNumber": self.city_card_id })
data = await response.text()
#_LOGGER.debug(data)
raw_data = BeautifulSoup(data, 'html.parser')
self.extract_date(raw_data)
if self.days() == 0:
self._state = STATE_OFF
else:
self._state = STATE_ON
except (asyncio.TimeoutError, aiohttp.ClientError) as error:
_LOGGER.error("Failed getting kkm information: %s", error)
示例14: get_config_via_legacy_route
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def get_config_via_legacy_route(bf_url, project_id):
from rasa.utils.endpoints import EndpointConfig
import aiohttp
response = {}
base_url = f"{bf_url}/project/{project_id}"
for endpoint in ["credentials", "endpoints"]:
server = EndpointConfig(url=f"{base_url}/{endpoint}")
async with server.session() as session:
params = server.combine_parameters()
url = server.url
@auto_retry
async def load():
try:
return await session.request(
"GET", url, timeout=DEFAULT_REQUEST_TIMEOUT, params=params
)
except aiohttp.ClientError:
return None
data = await load()
response[endpoint] = await data.json()
return response
示例15: wait_til_server_is_running
# 需要导入模块: import aiohttp [as 别名]
# 或者: from aiohttp import ClientError [as 别名]
def wait_til_server_is_running(
endpoint, max_retries=30, sleep_between_retries=1
) -> bool:
"""Try to reach the server, retry a couple of times and sleep in between."""
while max_retries:
try:
r = await retrieve_status(endpoint)
logger.info(f"Reached core: {r}")
if not r.get("is_ready"):
# server did not finish loading the agent yet
# in this case, we need to wait till the model trained
# so we might be sleeping for a while...
await asyncio.sleep(sleep_between_retries)
continue
else:
# server is ready to go
return True
except ClientError:
max_retries -= 1
if max_retries:
await asyncio.sleep(sleep_between_retries)
return False