Python requests.RequestException() Examples

The following are 30 code examples for showing how to use requests.RequestException(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module requests , or try the search function .

Example 1
Project: wechatpy   Author: wechatpy   File: base.py    License: MIT License 6 votes vote down vote up
def _fetch_access_token(self, url, params):
        """ The real fetch access token """
        logger.info("Fetching access token")
        res = self._http.get(url=url, params=params)
        try:
            res.raise_for_status()
        except requests.RequestException as reqe:
            raise WeChatClientException(
                errcode=None, errmsg=None, client=self, request=reqe.request, response=reqe.response,
            )
        result = res.json()
        if "errcode" in result and result["errcode"] != 0:
            raise WeChatClientException(
                result["errcode"], result["errmsg"], client=self, request=res.request, response=res,
            )

        expires_in = 7200
        if "expires_in" in result:
            expires_in = result["expires_in"]
        self.session.set(self.access_token_key, result["access_token"], expires_in)
        self.expires_at = int(time.time()) + expires_in
        return result 
Example 2
Project: wechatpy   Author: wechatpy   File: component.py    License: MIT License 6 votes vote down vote up
def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(("http://", "https://")):
            api_base_url = kwargs.pop("api_base_url", self.API_BASE_URL)
            url = f"{api_base_url}{url_or_endpoint}"
        else:
            url = url_or_endpoint

        if "params" not in kwargs:
            kwargs["params"] = {}
        if isinstance(kwargs["params"], dict) and "component_access_token" not in kwargs["params"]:
            kwargs["params"]["component_access_token"] = self.access_token
        if isinstance(kwargs["data"], dict):
            kwargs["data"] = json.dumps(kwargs["data"])

        res = self._http.request(method=method, url=url, **kwargs)
        try:
            res.raise_for_status()
        except requests.RequestException as reqe:
            raise WeChatClientException(
                errcode=None, errmsg=None, client=self, request=reqe.request, response=reqe.response,
            )

        return self._handle_result(res, method, url, **kwargs) 
Example 3
Project: wechatpy   Author: wechatpy   File: component.py    License: MIT License 6 votes vote down vote up
def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(("http://", "https://")):
            url = f"{self.API_BASE_URL}{url_or_endpoint}"
        else:
            url = url_or_endpoint

        if isinstance(kwargs.get("data", ""), dict):
            body = json.dumps(kwargs["data"], ensure_ascii=False)
            body = body.encode("utf-8")
            kwargs["data"] = body

        res = self._http.request(method=method, url=url, **kwargs)
        try:
            res.raise_for_status()
        except requests.RequestException as reqe:
            raise WeChatOAuthException(
                errcode=None, errmsg=None, client=self, request=reqe.request, response=reqe.response,
            )

        return self._handle_result(res, method=method, url=url, **kwargs) 
Example 4
Project: operator-courier   Author: operator-framework   File: push.py    License: Apache License 2.0 6 votes vote down vote up
def _push_to_registry(self, namespace, repository, release, bundle, auth_token):
        push_uri = 'https://quay.io/cnr/api/v1/packages/%s/%s' % (namespace, repository)
        logger.info('Pushing bundle to %s' % push_uri)
        headers = {'Content-Type': 'application/json', 'Authorization': auth_token}
        json = {'blob': bundle, 'release': release, "media_type": "helm"}

        try:
            r = requests.post(push_uri, json=json, headers=headers)
        except requests.RequestException as e:
            msg = str(e)
            logger.error(msg)
            raise OpCourierQuayCommunicationError(msg)

        if r.status_code != 200:
            logger.error(r.text)

            try:
                r_json = r.json()
            except ValueError:
                r_json = {}

            msg = r_json.get('error', {}).get(
                'message', 'Failed to get error details.'
            )
            raise OpCourierQuayErrorResponse(msg, r.status_code, r_json) 
Example 5
Project: python-nomad   Author: jrxFive   File: test_base.py    License: MIT License 6 votes vote down vote up
def test_base_raise_exception_not_requests_response_object(mock_requests):
    mock_requests().delete.side_effect = [requests.RequestException()]

    with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo:
        n = nomad.Nomad(
            host="162.16.10.102",
            port=common.NOMAD_PORT,
            timeout=0.001,
            verify=False
        )

        _ = n.job.deregister_job("example")

    # excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised.
    # The main attributes of interest are .type, .value and .traceback.
    # https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions
    assert hasattr(excinfo.value.nomad_resp, "text") is False
    assert isinstance(excinfo.value.nomad_resp, requests.RequestException)
    assert "raised due" in str(excinfo) 
Example 6
Project: python-tbk   Author: cornershop   File: zeep_client.py    License: MIT License 6 votes vote down vote up
def request(self, request, timeout=None):
        try:
            timeout = timeout or self.transport_timeout
            with self.transport.settings(timeout=timeout):
                method = self.get_method(request.method_name)
                result = method(*request.args, **request.kwargs)
        except zeep.exceptions.Fault as fault:
            self.logger.exception("Fault")
            error, code = parse_tbk_error_message(fault.message)
            raise SoapServerException(error, code, request)
        except RequestException as error:
            self.logger.exception("Request exception")
            raise SoapRequestException(error, request)
        else:
            serialized = zeep.helpers.serialize_object(result)
            last_sent = self.get_last_sent_envelope()
            last_received = self.get_last_received_envelope()
            return serialized, last_sent, last_received 
Example 7
Project: Auto_Record_Matsuri   Author: fzxiao233   File: tools.py    License: MIT License 6 votes vote down vote up
def bot(message: str, user_config: dict):
    if config['enable_bot'] and user_config['bot_notice']:
        try:
            group_id = user_config['group_id']
        except KeyError:
            group_id = config['group_id']
        # 传入JSON时,应使用这个UA
        headers = {'Content-Type': 'application/json',
                   'Authorization': f'Bearer {config["bot_token"]}'}
        for _group_id in group_id:
            _msg = {
                'group_id': int(_group_id),
                'message': message,
                'auto_escape': False
            }
            msg = json.dumps(_msg)
            logger = logging.getLogger('run.bot')
            try:
                requests.post(f'http://{config["bot_host"]}/send_group_msg', data=msg, headers=headers)
                logger.warning(f'{msg}')
            except requests.exceptions.RequestException as e:
                logger.exception(e) 
Example 8
Project: x-proxies   Author: cls1991   File: util.py    License: Apache License 2.0 6 votes vote down vote up
def request_html(url, method='GET', headers=None, proxies=None):
    """
    :param url:
    :param method:
    :param headers:
    :param proxies:
    :return:
    """
    resp = None
    try:
        r = requests.request(method, url, headers=headers, proxies=proxies)
        if r.status_code == 200:
            resp = r.text
    except requests.RequestException as e:
        print(e)

    return resp 
Example 9
Project: insightconnect-plugins   Author: rapid7   File: util.py    License: MIT License 6 votes vote down vote up
def get_password(self, hash_start: str) -> list:
        """
        :param hash_start: The first 5 characters of a SHA1 hash
        :return: A list of hashes that match the hash_start param
        """
        BASE_URl = 'https://api.pwnedpasswords.com/range/'

        url = BASE_URl + hash_start
        try:
            response = requests.get(url)
        except requests.RequestException as e:
            self.logger.error(e)
            raise
        hash_list = response.text.splitlines()
        hash_list = [hash_start + hash_ for hash_ in hash_list]
        hash_list = [hash_[:40] for hash_ in hash_list]
        return hash_list 
Example 10
Project: monasca-docker   Author: monasca   File: mysql_init.py    License: Apache License 2.0 6 votes vote down vote up
def retry(retries=5, delay=2.0,
          exc_types=(OperationalError, RequestException)):

    def decorator(func):
        def f_retry(*args, **kwargs):
            for i in range(retries):
                try:
                    return func(*args, **kwargs)
                except exc_types as exc:
                    if i < retries - 1:
                        logger.info('%s failed (attempt %d of %d)',
                                    func.__name__, i + 1, retries)
                        logger.debug('Caught exception, retrying...',
                                     exc_info=True)
                        time.sleep(delay)
                    else:
                        logger.exception('Failed after %d attempts', retries)
                        if isinstance(exc, RequestException):
                            logger.debug('Response was: %r', exc.response.text)

                        raise
        return f_retry
    return decorator 
Example 11
Project: monasca-docker   Author: monasca   File: keystone_init.py    License: Apache License 2.0 6 votes vote down vote up
def retry(retries=5, delay=2.0,
          exc_types=(RetriableConnectionFailure, RequestException)):

    def decorator(func):
        def f_retry(*args, **kwargs):
            for i in range(retries):
                try:
                    return func(*args, **kwargs)
                except exc_types as exc:
                    if i < retries - 1:
                        logger.debug('Caught exception, retrying...',
                                     exc_info=True)
                        time.sleep(delay)
                    else:
                        logger.exception('Failed after %d attempts', retries)
                        if isinstance(exc, RequestException):
                            logger.debug('Response was: %r', exc.response.text)

                        raise
        return f_retry
    return decorator 
Example 12
Project: monasca-docker   Author: monasca   File: grafana.py    License: Apache License 2.0 6 votes vote down vote up
def retry(retries=5, delay=2.0, exc_types=(RequestException,)):
    def decorator(func):
        def f_retry(*args, **kwargs):
            for i in range(retries):
                try:
                    return func(*args, **kwargs)
                except exc_types as exc:
                    if i < retries - 1:
                        logger.debug('Caught exception, retrying...',
                                     exc_info=True)
                        time.sleep(delay)
                    else:
                        logger.exception('Failed after %d attempts', retries)
                        if isinstance(exc, RequestException):
                            logger.debug('Response was: %r', exc.response.text)

                        raise
        return f_retry
    return decorator 
Example 13
Project: pygerrit2   Author: dpursehouse   File: __init__.py    License: MIT License 6 votes vote down vote up
def get(self, endpoint, return_response=False, **kwargs):
        """Send HTTP GET to the endpoint.

        :arg str endpoint: The endpoint to send to.
        :arg bool return_response: If true will also return the response

        :returns:
            JSON decoded result.

        :raises:
            requests.RequestException on timeout or connection error.

        """
        args = self.translate_kwargs(**kwargs)
        response = self.session.get(self.make_url(endpoint), **args)

        decoded_response = _decode_response(response)

        if return_response:
            return decoded_response, response
        return decoded_response 
Example 14
Project: pygerrit2   Author: dpursehouse   File: __init__.py    License: MIT License 6 votes vote down vote up
def put(self, endpoint, return_response=False, **kwargs):
        """Send HTTP PUT to the endpoint.

        :arg str endpoint: The endpoint to send to.

        :returns:
            JSON decoded result.

        :raises:
            requests.RequestException on timeout or connection error.

        """
        args = self.translate_kwargs(**kwargs)
        response = self.session.put(self.make_url(endpoint), **args)

        decoded_response = _decode_response(response)

        if return_response:
            return decoded_response, response
        return decoded_response 
Example 15
Project: pygerrit2   Author: dpursehouse   File: __init__.py    License: MIT License 6 votes vote down vote up
def post(self, endpoint, return_response=False, **kwargs):
        """Send HTTP POST to the endpoint.

        :arg str endpoint: The endpoint to send to.

        :returns:
            JSON decoded result.

        :raises:
            requests.RequestException on timeout or connection error.

        """
        args = self.translate_kwargs(**kwargs)
        response = self.session.post(self.make_url(endpoint), **args)

        decoded_response = _decode_response(response)

        if return_response:
            return decoded_response, response
        return decoded_response 
Example 16
Project: pygerrit2   Author: dpursehouse   File: __init__.py    License: MIT License 6 votes vote down vote up
def delete(self, endpoint, return_response=False, **kwargs):
        """Send HTTP DELETE to the endpoint.

        :arg str endpoint: The endpoint to send to.

        :returns:
            JSON decoded result.

        :raises:
            requests.RequestException on timeout or connection error.

        """
        args = self.translate_kwargs(**kwargs)
        response = self.session.delete(self.make_url(endpoint), **args)

        decoded_response = _decode_response(response)

        if return_response:
            return decoded_response, response
        return decoded_response 
Example 17
Project: pygerrit2   Author: dpursehouse   File: __init__.py    License: MIT License 6 votes vote down vote up
def review(self, change_id, revision, review):
        """Submit a review.

        :arg str change_id: The change ID.
        :arg str revision: The revision.
        :arg str review: The review details as a :class:`GerritReview`.

        :returns:
            JSON decoded result.

        :raises:
            requests.RequestException on timeout or connection error.

        """
        endpoint = "changes/%s/revisions/%s/review" % (change_id, revision)
        return self.post(
            endpoint, data=str(review), headers={"Content-Type": "application/json"}
        ) 
Example 18
Project: jira   Author: pycontribs   File: client.py    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _check_update_(self):
        """Check if the current version of the library is outdated."""
        try:
            data = requests.get(
                "https://pypi.python.org/pypi/jira/json", timeout=2.001
            ).json()

            released_version = data["info"]["version"]
            if parse_version(released_version) > parse_version(__version__):
                warnings.warn(
                    "You are running an outdated version of Jira Python %s. Current version is %s. Do not file any bugs against older versions."
                    % (__version__, released_version)
                )
        except requests.RequestException:
            pass
        except Exception as e:
            logging.warning(e) 
Example 19
Project: safe-relay-service   Author: gnosis   File: send_slack_notification.py    License: MIT License 6 votes vote down vote up
def handle(self, *args, **options):
        ethereum_client = EthereumClientProvider()
        app_name = apps.get_app_config('relay').verbose_name
        network_name = ethereum_client.get_network().name.capitalize()
        startup_message = f'Starting {app_name} version {__version__} on {network_name}'
        self.stdout.write(self.style.SUCCESS(startup_message))

        if settings.SLACK_API_WEBHOOK:
            try:
                r = requests.post(settings.SLACK_API_WEBHOOK, json={'text': startup_message})
                if r.ok:
                    self.stdout.write(self.style.SUCCESS(f'Slack configured, "{startup_message}" sent'))
                else:
                    raise RequestException()
            except RequestException:
                self.stdout.write(self.style.ERROR('Cannot send slack notification'))
        else:
            self.stdout.write(self.style.SUCCESS('Slack not configured, ignoring')) 
Example 20
Project: idunn   Author: QwantResearch   File: recycling.py    License: Apache License 2.0 6 votes vote down vote up
def from_es(cls, place, lang):
        if not recycling_client.enabled:
            # Data source is not configured
            return None

        if place.PLACE_TYPE != "poi":
            return None

        if place.get_class_name() != "recycling":
            return None

        if not is_poi_in_finistere(place):
            return None

        try:
            containers = cls.fetch_containers(place)
        except (RequestException, ValidationError):
            logger.warning("Failed to fetch recycling containers data", exc_info=True)
            return None

        if not containers:
            return None
        return cls(containers=containers) 
Example 21
Project: pwned-passwords-django   Author: ubernostrum   File: api.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _get_pwned(prefix: str) -> Optional[Dict[str, int]]:
    """
    Fetches a dict of all hash suffixes from Pwned Passwords for a
    given SHA-1 prefix.

    """
    try:
        response = requests.get(
            url=API_ENDPOINT.format(prefix),
            headers={"User-Agent": USER_AGENT},
            timeout=getattr(settings, "PWNED_PASSWORDS_API_TIMEOUT", REQUEST_TIMEOUT),
        )
        response.raise_for_status()
    except requests.RequestException as e:
        # Gracefully handle timeouts and HTTP error response codes.
        log.warning("Skipped Pwned Passwords check due to error: %r", e)
        return None

    results = {}
    for line in response.text.splitlines():
        line_suffix, _, times = line.partition(":")
        results[line_suffix] = int(times)

    return results 
Example 22
Project: black-widow   Author: offensive-hub   File: sqlmaptask.py    License: GNU General Public License v3.0 6 votes vote down vote up
def _request(url: str, request_type: str = HttpRequest.Type.GET, json: dict or list = None) -> dict:
        """
        Send a request to sqlmap-api server and then load the data json as dict
        :param url: The sqlmap-api url (eg. "http://127.0.0.1:8775/task/new")
        :param request_type: get|post|put|patch|delete
        :param json: The json to send
        :rtype: dict
        """
        response = HttpRequest.request(url, request_type, json=json)
        r_data = JsonSerializer.load_json(response.text)
        Log.info('Response data of ' + url + ': ' + str(r_data))
        success = r_data.get('success')
        if success is None:
            # The response has not the status management
            return r_data
        if not success:
            Log.error('Response data of ' + url + ' has { success: False }')
            raise requests.RequestException('Request to ' + url + ' failed')
        return r_data 
Example 23
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def _sequence_page(self, user_name, page) -> ([OSCSequence], Exception):
        try:
            parameters = {'ipp': 100,
                          'page': page,
                          'username': user_name}
            login_url = OSCApiMethods.user_sequences(self.environment)
            response = requests.post(url=login_url, data=parameters)
            json_response = response.json()

            sequences = []
            if 'currentPageItems' in json_response:
                items = json_response['currentPageItems']
                for item in items:
                    sequence = OSCSequence.sequence_from_json(item)
                    sequences.append(sequence)

            return sequences, None
        except requests.RequestException as ex:
            return None, ex 
Example 24
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def get_photos(self, sequence_id: int) -> ([OSCPhoto], Exception):
        """this method will return a list of photo objects for a sequence id"""
        try:
            parameters = {'sequenceId': sequence_id}
            login_url = OSCApiMethods.photo_list(self.environment)
            response = requests.post(url=login_url, data=parameters)
            json_response = response.json()
            missing_field = None
            if 'osv' not in json_response:
                missing_field = "osv"
            elif 'photos' not in json_response['osv']:
                missing_field = "photos"
            else:
                photos = []
                photos_json = json_response['osv']['photos']
                for photo_json in photos_json:
                    photo = OSCPhoto.photo_from_json(photo_json)
                    photos.append(photo)
                return photos, missing_field
        except requests.RequestException as ex:
            return [], ex
        return [], Exception("OSC API bug. OSCPhoto missing field:" + missing_field) 
Example 25
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def get_image(self, photo: OSCPhoto, path: str, override=False) -> Exception:
        """downloads the image at the path specified"""
        jpg_name = path + '/' + str(photo.sequence_index) + '.jpg'
        if not override and os.path.isfile(jpg_name):
            return None

        try:
            response = requests.get(OSCApiMethods.resource(self.environment,
                                                           photo.image_name),
                                    stream=True)
            if response.status_code == 200:
                with open(jpg_name, 'wb') as file:
                    response.raw.decode_content = True
                    shutil.copyfileobj(response.raw, file)
        except requests.RequestException as ex:
            return ex

        return None 
Example 26
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def download_metadata(self, sequence: OSCSequence, path: str, override=False):
        """this method will download a metadata file of a sequence to the specified path.
        If there is a metadata file at that path by default no override will be made."""
        if sequence.metadata_url is None:
            return None
        metadata_path = path + "/track.txt"
        if not override and os.path.isfile(metadata_path):
            return None

        try:
            response = requests.get(OSCApiMethods.resource(self.environment,
                                                           sequence.metadata_url),
                                    stream=True)
            if response.status_code == 200:
                with open(metadata_path, 'wb') as file:
                    response.raw.decode_content = True
                    shutil.copyfileobj(response.raw, file)
        except requests.RequestException as ex:
            return ex

        return None 
Example 27
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def finish_upload(self, sequence: OSCSequence, token: str) -> (bool, Exception):
        """this method must be called in order to signal that a sequence has no more data to be
        uploaded."""
        try:
            parameters = {'sequenceId': sequence.online_id,
                          'access_token': token}
            response = requests.post(OSCApiMethods.finish_upload(self.environment),
                                     data=parameters)
            json_response = response.json()
            if "status" not in json_response:
                # we don't have a proper status documentation
                return False, None
            return True, None
        except requests.RequestException as ex:
            return None, ex

        return None, None 
Example 28
Project: upload-scripts   Author: openstreetcam   File: osc_api_gateway.py    License: MIT License 6 votes vote down vote up
def upload_video(self, access_token,
                     sequence_id,
                     video_path: str,
                     video_index) -> (bool, Exception):
        """This method will upload a video to OSC API"""
        try:
            parameters = {'access_token': access_token,
                          'sequenceId': sequence_id,
                          'sequenceIndex': video_index
                          }
            load_data = {'video': (os.path.basename(video_path),
                                   open(video_path, 'rb'),
                                   'video/mp4')}
            video_upload_url = OSCApiMethods.video_upload(self.environment)
            response = requests.post(video_upload_url,
                                     data=parameters,
                                     files=load_data,
                                     timeout=100)
            return OSCApi.__upload_response_success(response, "video", video_index), None
        except requests.RequestException as ex:
            LOGGER.debug("Received exception on video upload %s", str(ex))
            return False, ex 
Example 29
Project: python-zillow   Author: seme0021   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def _RequestUrl(self, url, verb, data=None):
        """
        Request a url.
        :param url: The web location we want to retrieve.
        :param verb: GET only (for now).
        :param data: A dict of (str, unicode) key/value pairs.
        :return:A JSON object.
        """
        if verb == 'GET':
            url = self._BuildUrl(url, extra_params=data)
            try:
                return requests.get(
                    url,
                    auth=self.__auth,
                    timeout=self._timeout
                )
            except requests.RequestException as e:
                raise ZillowError(str(e))
        return 0 
Example 30
Project: jet-bridge   Author: jet-admin   File: check_token.py    License: MIT License 6 votes vote down vote up
def check_token_command(api_url):
    try:
        if not is_token_activated(settings.TOKEN):
            logger.warning('[!] Your server token is not activated')
            logger.warning('[!] Token: {}'.format(settings.TOKEN))

            if settings.DATABASE_ENGINE != 'none' and settings.AUTO_OPEN_REGISTER and api_url.startswith('http'):
                register_url = '{}register/'.format(api_url)

                if settings.TOKEN:
                    register_url += '?token={}'.format(settings.TOKEN)

                if webbrowser.open(register_url):
                    logger.warning('[!] Activation page was opened in your browser - {}'.format(register_url))
            else:
                register_url = '{}register/'.format(api_url)
                logger.warning('[!] Go to {} to activate it'.format(register_url))
    except RequestException:
        logger.error('[!] Can\'t connect to Jet Admin API')
        logger.error('[!] Token verification failed')