Python源码示例:aiohttp.ClientError()

示例1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 
示例16
def async_check_http_oauth(self, triggered=None):
        _LOGGER.debug("[%s] check accessibility from local", LOGGER_NAME)
        try:
            if self._retry_remove is not None:
                self._retry_remove()
                self._retry_remove = None

            session = async_get_clientsession(self._hass, verify_ssl=False)
            with async_timeout.timeout(5, loop= self._hass.loop):
                response = await session.get(self._ha_url + '/havcs/auth/authorize')
            if response.status == 401:
                _LOGGER.debug("[%s][check] access success: url = %s, status = %s", LOGGER_NAME, self._ha_url + '/havcs/auth/authorize', response.status)
        except (asyncio.TimeoutError, aiohttp.ClientError):
            _LOGGER.debug("[%s][check] retry check after 15s", LOGGER_NAME)
            self._retry_times -= 1
            if(self._retry_times > 0):
                self._retry_remove = async_track_time_interval(
                    self._hass, self.async_check_http_oauth, timedelta(seconds=15)
                )
            else:
                _LOGGER.error("[%s][check] can not access http, check `ha_url` in configuration.yml", LOGGER_NAME)
        except Exception:
            _LOGGER.exception("[%s][check] unexpected error occur", LOGGER_NAME)
            raise 
示例17
def test_exceptions(self):
        import aiohttp

        exceptions = aiohttp_.AiohttpClient.exceptions

        with pytest.raises(exceptions.BaseClientException):
            raise aiohttp.ClientError()

        with pytest.raises(exceptions.BaseClientException):
            # Test polymorphism
            raise aiohttp.InvalidURL("invalid")

        with pytest.raises(exceptions.ConnectionError):
            raise aiohttp.ClientConnectionError()

        with pytest.raises(exceptions.ConnectionTimeout):
            raise aiohttp.ClientConnectorError.__new__(
                aiohttp.ClientConnectorError
            )

        with pytest.raises(exceptions.ServerTimeout):
            raise aiohttp.ServerTimeoutError()

        with pytest.raises(exceptions.SSLError):
            raise aiohttp.ClientSSLError.__new__(aiohttp.ClientSSLError)

        with pytest.raises(exceptions.InvalidURL):
            raise aiohttp.InvalidURL("invalid") 
示例18
def get_access_token(self) -> None:
        """
        Get a Reddit API OAuth2 access token and assign it to self.access_token.

        A token is valid for 1 hour. There will be MAX_RETRIES to get a token, after which the cog
        will be unloaded and a ClientError raised if retrieval was still unsuccessful.
        """
        for i in range(1, self.MAX_RETRIES + 1):
            response = await self.bot.http_session.post(
                url=f"{self.URL}/api/v1/access_token",
                headers=self.HEADERS,
                auth=self.client_auth,
                data={
                    "grant_type": "client_credentials",
                    "duration": "temporary"
                }
            )

            if response.status == 200 and response.content_type == "application/json":
                content = await response.json()
                expiration = int(content["expires_in"]) - 60  # Subtract 1 minute for leeway.
                self.access_token = AccessToken(
                    token=content["access_token"],
                    expires_at=datetime.utcnow() + timedelta(seconds=expiration)
                )

                log.debug(f"New token acquired; expires on UTC {self.access_token.expires_at}")
                return
            else:
                log.debug(
                    f"Failed to get an access token: "
                    f"status {response.status} & content type {response.content_type}; "
                    f"retrying ({i}/{self.MAX_RETRIES})"
                )

            await asyncio.sleep(3)

        self.bot.remove_cog(self.qualified_name)
        raise ClientError("Authentication with the Reddit API failed. Unloading the cog.") 
示例19
def __init__(self, coro, seconds, hours, minutes, count, reconnect, loop):
        self.coro = coro
        self.reconnect = reconnect
        self.loop = loop or asyncio.get_event_loop()
        self.count = count
        self._current_loop = 0
        self._task = None
        self._injected = None
        self._valid_exception = (
            OSError,
            discord.HTTPException,
            discord.GatewayNotFound,
            discord.ConnectionClosed,
            aiohttp.ClientError,
            asyncio.TimeoutError,
            websockets.InvalidHandshake,
            websockets.WebSocketProtocolError,
        )

        self._before_loop = None
        self._after_loop = None
        self._is_being_cancelled = False
        self._has_failed = False
        self._stop_next_iteration = False

        if self.count is not None and self.count <= 0:
            raise ValueError('count must be greater than 0 or None.')

        self.change_interval(seconds=seconds, minutes=minutes, hours=hours)
        self._last_iteration = None
        self._next_iteration = None

        if not inspect.iscoroutinefunction(self.coro):
            raise TypeError('Expected coroutine function, not {0.__name__!r}.'.format(type(self.coro))) 
示例20
def store_async(url: str, path: str) -> aiohttp.ClientResponse:
    print('Async storing {url} in {path}'.format(url=url, path=path))
    try:
        async with aiohttp.ClientSession() as aios:
            response = await aios.get(url)
            with open(path, 'wb') as fout:
                while True:
                    chunk = await response.content.read(1024)
                    if not chunk:
                        break
                    fout.write(chunk)
            return response
    # type: ignore # urllib isn't fully stubbed
    except (urllib.error.HTTPError, aiohttp.ClientError) as e:
        raise FetchException(e) 
示例21
def request(session, oauth_token, skill_id=None, method=None, json=None,
                  file=None, request_method='POST', custom_url=None, **kwargs):
    """
    Make a request to API

    :param session: HTTP Client session
    :type session: :obj:`aiohttp.ClientSession`
    :param oauth_token: oauth_token
    :type oauth_token: :obj:`str`
    :param skill_id: skill_id. Optional. Not used if custom_url is provided
    :type skill_id: :obj:`str`
    :param method: API method. Optional. Not used if custom_url is provided
    :type method: :obj:`str`
    :param json: request payload
    :type json: :obj: `dict`
    :param file: file
    :type file: :obj: `io.BytesIO`
    :param request_method: API request method
    :type request_method: :obj:`str`
    :param custom_url: Yandex has very developer UNfriendly API, so some endpoints cannot be achieved by standatd template.
    :type custom_url: :obj:`str`
    :return: result
    :rtype: ::obj:`dict`
    """
    log.debug("Making a `%s` request to %r with json `%r` or file `%r`",
              request_method, method, json, file)
    if custom_url is None:
        url = Methods.api_url(skill_id, method)
    else:
        url = custom_url
    headers = {'Authorization': oauth_token}
    data = None
    if file:
        data = aiohttp.FormData()
        data.add_field('file', file)
    try:
        async with session.request(request_method, url, json=json, data=data, headers=headers, **kwargs) as response:
            return await _check_result(response)
    except aiohttp.ClientError as e:
        raise exceptions.NetworkError(f"aiohttp client throws an error: {e.__class__.__name__}: {e}") 
示例22
def fetch(self, uri):
        sleep_times = self.sleeps(uri)
        self._exns = set()
        self._state = state.green
        for s in sleep_times:
            headers = {"Accept": "application/json"}
            params = {"embed": "body"}
            try:
                async with self.session.get(uri, params=params, headers=headers) as response:
                    # 200 OK? Return the Page
                    if(response.status == 200):
                        self._log.debug("Fetched %s", uri)
                        js = await response.json()
                        return Page(js, self._log)
            # Wonky URI? Raise to caller
            except ValueError as e:
                raise UrlError(e)
            # Error from the HTTP response?
            except aiohttp.ClientResponseError as e:
                self.log(e, uri)
                # For a 404, raise HttpNotFound
                if e.code == 404:
                    raise HttpNotFoundError(uri, 404)
                # For a client error other than a timeout, raise HttpClientError
                # Timeouts should log and sleep
                if e.code < 500 and e.code != 408:
                    raise HttpClientError(uri, e.code)
            # Other connection errors and malformed payloads just log and sleep
            except (aiohttp.ClientError) as e:
                self.log(e, uri)
            # Http timeout? Log and sleep
            except asyncio.TimeoutError as e:
                self.log(e, uri)

            await self.sleep(s) 
示例23
def _async_get_wu_data(hass, session, api_key, features, query):
    try:
        with async_timeout.timeout(9, loop=hass.loop):
            resp = await session.get(_WU_API_URL.format(
                api_key=api_key, features='/'.join(features), query=query))
        resp.raise_for_status()
        resp = await resp.json()
        if 'error' in resp['response']:
            raise ValueError('Error from api.wunderground.com: {}'.format(
                resp['response']['error']['description']))
    except (aiohttp.ClientError, asyncio.TimeoutError, ValueError) as exc:
        _LOGGER.error('{}: {}'.format(exc.__class__.__name__, str(exc)))
        return None

    return resp 
示例24
def post_json(self, request_json: dict) -> dict:
        self._change_url_by_sandbox()
        try:
            async with self._session.post(
                self.url, json=request_json, timeout=ClientTimeout(total=self.http_timeout)
            ) as resp:
                return await resp.json(content_type=None)
        except (ValueError, ClientError):
            raise InAppPyValidationError("HTTP error") 
示例25
def _retrieve(self, url, requests_kwargs):
        try:
            resp = await self._session.get(url, headers={'accept':
                                                         PEM_FILE_TYPE},
                                           **requests_kwargs)
            resp.raise_for_status()
            self._check_content_type(url, resp.headers['content-type'])
            return await resp.text()
        except aiohttp.ClientError as e:
            status_code = getattr(e, 'code', None)
            raise PublicKeyRetrieverException(e, status_code=status_code) 
示例26
def send_action(
    endpoint: EndpointConfig,
    sender_id: Text,
    action_name: Text,
    policy: Optional[Text] = None,
    confidence: Optional[float] = None,
    is_new_action: bool = False
) -> Dict[Text, Any]:
    """Log an action to a conversation."""

    payload = ActionExecuted(action_name, policy, confidence).as_dict()

    subpath = "/conversations/{}/execute".format(sender_id)

    try:
        return await endpoint.request(json=payload,
                                      method="post",
                                      subpath=subpath)
    except ClientError:
        if is_new_action:
            warning_questions = questionary.confirm(
                "WARNING: You have created a new action: '{}', "
                "which was not successfully executed. "
                "If this action does not return any events, "
                "you do not need to do anything. "
                "If this is a custom action which returns events, "
                "you are recommended to implement this action "
                "in your action server and try again."
                "".format(action_name))
            await _ask_questions(warning_questions, sender_id, endpoint)

            payload = ActionExecuted(action_name).as_dict()

            return await send_event(endpoint, sender_id, payload)
        else:
            logger.error("failed to execute action!")
            raise 
示例27
def async_get_longitude_latitude(self, address_dict):
            
        if address_dict.get(CONF_LONGITUDE_LATITUDE) is not None:
            return address_dict.get(CONF_LONGITUDE_LATITUDE)

        if (address_dict.get(CONF_ADDRESS) is None) or (address_dict.get(CONF_CITY) is None):
            return
            
        url = ("http://restapi.amap.com/v3/geocode/geo?key="
               + self._api_key
               + '&address=' + address_dict.get(CONF_ADDRESS)
               + '&city=' + address_dict.get(CONF_CITY)
               )

        try:
            session = async_get_clientsession(self._hass)
            with async_timeout.timeout(15, loop=self._hass.loop):
                response = yield from session.get( url )

        except(asyncio.TimeoutError, aiohttp.ClientError):
            _LOGGER.error("Error while accessing: %s", url)
            return

        if response.status != 200:
            _LOGGER.error("Error while accessing: %s, status=%d", url, response.status)
            return

        data = yield from response.json()

        if data is None:
            _LOGGER.error("Request api Error: %s", url)
            return
        elif (data['status'] != '1'):
            _LOGGER.error("Error Api return, state=%s, errmsg=%s",
                          data['status'],
                          data['info']
                          )
            return

        return data['geocodes'][0]['location'] 
示例28
def url_check(url):
    try:
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as resp:
                return resp.status == 200
    except aiohttp.ClientError:
        return False 
示例29
def _group_send(self, dst_url, messages):
        with self._sentry_group_message_scope(dst_url):
            LOG.info(f'send {len(messages)} messages to {dst_url}')
            data = ActorMessage.batch_encode(messages, self.content_encoding)
            try:
                async with self.session.post(dst_url, data=data, headers=self.headers) as r:
                    await r.read()
            except aiohttp.ClientConnectionError as ex:
                LOG.warning(f'failed to send message to {dst_url}: {ex}')
                return
            except aiohttp.ClientError as ex:
                LOG.warning(f'failed to send message to {dst_url}: {ex}')
                raise
            aiohttp_raise_for_status(r) 
示例30
def ask(self, message: ActorMessage):
        await self._async_init()
        message, dst_url, headers, data = self._get_ask_request(message)
        with self._sentry_message_scope(message):
            try:
                async with self.session.post(dst_url, data=data, headers=headers) as r:
                    headers = r.headers
                    content = await r.read()
            except aiohttp.ClientError as ex:
                LOG.warning(f'failed to send message to {dst_url}: {ex}')
                raise
            aiohttp_raise_for_status(r)
            return self._decode_ask_response(content, headers)