How to use the requests.exceptions function in requests

To help you get started, we’ve selected a few requests examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rusoto / rusoto / integration_tests / docker_test_run.py View on Github external
def _wait_for_s3(self):
        while True:
            time.sleep(1)
            try:
                requests.get("http://localhost:{}".format(self.port))
                break
            except requests.exceptions.ConnectionError:
                print("waiting for container to become ready", self)

        print("container ready, waiting another 5 seconds to ensure everything is set up")
        time.sleep(5)
github MrNobodyCh / Feedler / botan.py View on Github external
def track(token, uid, message, name='Message'):
    try:
        r = requests.post(
            TRACK_URL,
            params={"token": token, "uid": uid, "name": name},
            data=json.dumps(message),
            headers={'Content-type': 'application/json'},
        )
        return r.json()
    except requests.exceptions.Timeout:
        # set up for a retry, or continue in a retry loop
        return False
    except (requests.exceptions.RequestException, ValueError) as e:
        # catastrophic error
        print(e)
        return False
github parltrack / parltrack / utils / utils.py View on Github external
def fetch_raw(url, retries=5, ignore=[], params=None, binary=False):
    try:
        if params:
            r=sess.post(url, params=params, headers=HEADERS)
        else:
            r=sess.get(url, headers=HEADERS)
    except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e:
        if e == requests.exceptions.Timeout:
            retries = min(retries, 1)
        if retries>0:
            time.sleep(4*(6-retries))
            return fetch_raw(url, retries-1, ignore=ignore, params=params, binary=binary)
        else:
            raise ValueError("failed to fetch %s" % url)
    if r.status_code >= 400 and r.status_code not in [504, 502]+ignore:
        r.raise_for_status()
    if binary: return r.content
    return r.text
github dwighthubbard / python-dlipower / dlipower / dlipower.py View on Github external
if name:
                fields[name] = value

        fields['Username'] = self.userid
        fields['Password'] = self.password

        form_response = fields['Challenge'] + fields['Username'] + fields['Password'] + fields['Challenge']

        m = hashlib.md5()
        m.update(form_response.encode())
        data = {'Username': 'admin', 'Password': m.hexdigest()}
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}

        try:
            response = self.session.post('%s/login.tgi' % self.base_url, headers=headers, data=data, timeout=self.timeout, verify=False)
        except requests.exceptions.ConnectTimeout:
            self.secure_login = False
            self.session = None
            return

        if response.status_code == 200:
            if 'Set-Cookie' in response.headers:
                self.secure_login = True
github Drakkar-Software / OctoBot-Tentacles / Services / Services_bases / twitter_service / twitter_service.py View on Github external
def get_successful_startup_message(self):
        try:
            return f"Successfully initialized and accessible at: {self._fetch_twitter_url()}.", True
        except requests.exceptions.ConnectionError as e:
            self.log_connection_error_message(e)
            return "", False
github skwashd / python-acquia-cloud / acapi / resources / acquiadata.py View on Github external
requests_cache.clear()

        if "DELETE" == method:
            resp = self.session.delete(uri, auth=self.auth, headers=headers, params=params)
            # Quickest and easiest way to do this.
            if self.is_cache_enabled():
                requests_cache.clear()

        if hasattr(resp, "from_cache") and resp.from_cache:
            LOGGER.info("%s %s returned from cache", method, uri)

        self.last_response = resp

        try:
            resp.raise_for_status()
        except requests.exceptions.HTTPError as exp:
            LOGGER.info(
                "Failed request response headers: \n%s",
                pformat(exp.response.headers, indent=2),
            )
            raise

        if stream:
            return resp

        if decode_json:
            return resp.json()

        return resp.content
github apache / airflow / airflow / hooks / http_hook.py View on Github external
def check_response(self, response):
        """
        Checks the status code and raise an AirflowException exception on non 2XX or 3XX
        status codes

        :param response: A requests response object
        :type response: requests.response
        """
        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError:
            self.log.error("HTTP error: %s", response.reason)
            self.log.error(response.text)
            raise AirflowException(str(response.status_code) + ":" + response.reason)
github gabstopper / smc-python / smc / api / session.py View on Github external
"""
        if not self.session:
            self.manager._deregister(self)
            return
        try:
            r = self.session.put(self.entry_points.get('logout'))
            if r.status_code == 204:
                logger.info('Logged out admin: %s of domain: %s successfully',
                    self.name, self.domain)
            else:
                logger.error('Logout status was unexpected. Received response '
                    'with status code: %s', (r.status_code))

        except requests.exceptions.SSLError as e:
            logger.error('SSL exception thrown during logout: %s', e)
        except requests.exceptions.ConnectionError as e:
            logger.error('Connection error on logout: %s', e)
        finally:
            self.entry_points.clear()
            self.manager._deregister(self)
            self._session = None
            try:
                delattr(self, 'current_user')
            except AttributeError:
                pass
        
        logger.debug('Call counters: %s' % counters)    
github quandl / quandl-python / quandl / connection.py View on Github external
def execute_request(cls, http_verb, url, **options):
        session = cls.get_session()

        try:
            response = session.request(method=http_verb,
                                       url=url,
                                       verify=ApiConfig.verify_ssl,
                                       **options)
            if response.status_code < 200 or response.status_code >= 300:
                cls.handle_api_error(response)
            else:
                return response
        except requests.exceptions.RequestException as e:
            if e.response:
                cls.handle_api_error(e.response)
            raise e
github AppScale / appscale-tools / appscale / tools / admin_api / client.py View on Github external
Args:
      response: A response object from the requests library.
    Returns:
      The response body as a dictionary.
    Raises:
      AdminError if the response indicates an unsuccessful request.
    """
    try:
      content = response.json()
    except ValueError:
      raise AdminError('Invalid response: {}'.format(response.content))

    try:
      response.raise_for_status()
    except requests.exceptions.HTTPError:
      try:
        message = content['error']['message']
      except KeyError:
        message = 'AdminServer returned: {}'.format(response.status_code)
      raise AdminError(message)

    return content