当前位置: 首页>>代码示例>>Python>>正文


Python aiohttp.ClientError方法代码示例

本文整理汇总了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() 
开发者ID:dongweiming,项目名称:mp,代码行数:24,代码来源:crawl.py

示例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 
开发者ID:dephell,项目名称:dephell,代码行数:19,代码来源:networking.py

示例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 
开发者ID:RasaHQ,项目名称:rasa_core,代码行数:26,代码来源:interactive.py

示例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 
开发者ID:anyant,项目名称:rssant,代码行数:18,代码来源:image_proxy.py

示例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', '/') 
开发者ID:aio-libs,项目名称:aioelasticsearch,代码行数:21,代码来源:test_connection.py

示例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 
开发者ID:claws,项目名称:dump1090-exporter,代码行数:23,代码来源:exporter.py

示例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)) 
开发者ID:nnewsom,项目名称:webbies,代码行数:19,代码来源:Bing.py

示例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 
开发者ID:NabDev,项目名称:NabBot,代码行数:19,代码来源:tibia.py

示例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 
开发者ID:NabDev,项目名称:NabBot,代码行数:21,代码来源:tibia.py

示例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) 
开发者ID:smlbiobot,项目名称:SML-Cogs,代码行数:25,代码来源:racf_audit.py

示例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) 
开发者ID:StellarCN,项目名称:py-stellar-base,代码行数:20,代码来源:aiohttp_client.py

示例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) 
开发者ID:macbury,项目名称:SmartHouse,代码行数:21,代码来源:sensor.py

示例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) 
开发者ID:macbury,项目名称:SmartHouse,代码行数:18,代码来源:sensor.py

示例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 
开发者ID:botfront,项目名称:rasa-for-botfront,代码行数:26,代码来源:botfront.py

示例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 
开发者ID:botfront,项目名称:rasa-for-botfront,代码行数:26,代码来源:interactive.py


注:本文中的aiohttp.ClientError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。