Python requests.exceptions.Timeout() Examples

The following are 30 code examples of requests.exceptions.Timeout(). 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 also want to check out all available functions/classes of the module requests.exceptions , or try the search function .
Example #1
Source File: http.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def request(self, url, *args, **kwargs):
        try:
            stream = HttpStream(url, *args, verbose=self.verbose, **kwargs)
            if kwargs.get('stream', False):
                return stream

            with stream as s:
                content = s.req.content
                return HttpResponse(s.status_code, s.headers, content)
        # Timeout will catch both ConnectTimout and ReadTimeout
        except (RetryError, Timeout) as ex:
            raise OsbsNetworkException(url, str(ex), '',
                                       cause=ex, traceback=sys.exc_info()[2])
        except HTTPError as ex:
            raise OsbsNetworkException(url, str(ex), ex.response.status_code,
                                       cause=ex, traceback=sys.exc_info()[2])
        except Exception as ex:
            raise OsbsException(cause=ex, traceback=sys.exc_info()[2]) 
Example #2
Source File: test_connector.py    From infoblox-client with Apache License 2.0 7 votes vote down vote up
def test_neutron_exception_is_raised_on_any_request_error(self):
        # timeout exception raises InfobloxTimeoutError
        f = mock.Mock()
        f.__name__ = 'mock'
        f.side_effect = req_exc.Timeout
        self.assertRaises(exceptions.InfobloxTimeoutError,
                          connector.reraise_neutron_exception(f))

        # all other request exception raises InfobloxConnectionError
        supported_exceptions = [req_exc.HTTPError,
                                req_exc.ConnectionError,
                                req_exc.ProxyError,
                                req_exc.SSLError,
                                req_exc.TooManyRedirects,
                                req_exc.InvalidURL]

        for ex in supported_exceptions:
            f.side_effect = ex
            self.assertRaises(exceptions.InfobloxConnectionError,
                              connector.reraise_neutron_exception(f)) 
Example #3
Source File: test_databricks.py    From airflow with Apache License 2.0 7 votes vote down vote up
def test_do_api_call_waits_between_retries(self, mock_sleep):
        retry_delay = 5
        self.hook = DatabricksHook(retry_delay=retry_delay)

        for exception in [requests_exceptions.ConnectionError,
                          requests_exceptions.SSLError,
                          requests_exceptions.Timeout,
                          requests_exceptions.ConnectTimeout,
                          requests_exceptions.HTTPError]:
            with mock.patch('airflow.providers.databricks.hooks.databricks.requests') as mock_requests:
                with mock.patch.object(self.hook.log, 'error'):
                    mock_sleep.reset_mock()
                    setup_mock_requests(mock_requests, exception)

                    with self.assertRaises(AirflowException):
                        self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {})

                    self.assertEqual(len(mock_sleep.mock_calls), self.hook.retry_limit - 1)
                    calls = [
                        mock.call(retry_delay),
                        mock.call(retry_delay)
                    ]
                    mock_sleep.assert_has_calls(calls) 
Example #4
Source File: ext_ip.py    From oschameleon with MIT License 6 votes vote down vote up
def _fetch_data(self, urls):
        # we only want warning+ messages from the requests module
        logging.getLogger("requests").setLevel(logging.WARNING)
        for url in urls:
            try:
                req = requests.get(url, timeout=3)
                if req.status_code == 200:
                    data = req.text.strip()
                    if data is None or not self._verify_address(data):
                        continue
                    else:
                        return data
                else:
                    raise ConnectionError
            except (Timeout, ConnectionError) as e:
                logger.warning('Could not fetch public ip from %s', url)
        return None 
Example #5
Source File: splash_browser.py    From transistor with MIT License 6 votes vote down vote up
def post(self, *args, **kwargs):
        """Straightforward wrapper around `requests.Session.post
        <http://docs.python-requests.org/en/master/api/#requests.Session.post>`__.

        :return: `requests.Response
            <http://docs.python-requests.org/en/master/api/#requests.Response>`__
            object with a *soup*-attribute added by :func:`_add_soup`.
        """

        try:
            response = self.session.post(*args, **kwargs)
            self._update_state(response)
            return response
        except Timeout:
            self.timeout_exception = True
            print(f'Timeout exception.')
            resp = Response()
            resp.status_code = 408
            self._update_state(resp)
            return resp 
Example #6
Source File: utils.py    From iquery with MIT License 6 votes vote down vote up
def requests_get(url, **kwargs):
    USER_AGENTS = (
        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0',
        'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100 101 Firefox/22.0',
        'Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0',
        ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) '
         'Chrome/19.0.1084.46 Safari/536.5'),
        ('Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46'
         'Safari/536.5')
    )
    try:
        r = requests.get(
            url,
            timeout=12,
            headers={'User-Agent': random.choice(USER_AGENTS)}, **kwargs
        )
    except ConnectionError:
        exit_after_echo('Network connection failed.')
    except Timeout:
        exit_after_echo('timeout.')
    return r 
Example #7
Source File: poll_pods.py    From social-relay with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_pod_relay_preferences(self, host):
        """Query remote pods on https first, fall back to http."""
        logging.info("Querying %s" % host)
        try:
            try:
                response = requests.get("https://%s/.well-known/x-social-relay" % host,
                                timeout=5,
                                headers={"User-Agent": config.USER_AGENT})
            except timeout:
                response = None
            if not response or response.status_code != 200:
                response = requests.get("http://%s/.well-known/x-social-relay" % host,
                                timeout=5,
                                headers={"User-Agent": config.USER_AGENT})
                if response.status_code != 200:
                    return None
        except (ConnectionError, Timeout, timeout):
            return None
        try:
            # Make sure we have a valid x-social-relay doc
            validate(response.json(), self.schema)
            return response.text
        except (ValueError, ValidationError):
            return None 
Example #8
Source File: main.py    From GetSubtitles with MIT License 6 votes vote down vote up
def get_search_results(self, video):
        results = OrderedDict()
        for i, downloader in enumerate(self.downloader):
            try:
                result = downloader.get_subtitles(video, sub_num=self.sub_num)
                results.update(result)
            except ValueError as e:
                print("error: " + str(e))
            except (exceptions.Timeout, exceptions.ConnectionError):
                print("connect timeout, search next site.")
                if i == (len(self.downloader) - 1):
                    print("PLEASE CHECK YOUR NETWORK STATUS")
                    sys.exit(0)
                else:
                    continue
            # TODO: search all sites or exit after collecting enough results
            if len(results) >= self.sub_num:
                break
        return results 
Example #9
Source File: test_salesforce.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def test_gather_logs_failed(self, mock_post, mock_get, mock_logger):
        """SalesforceApp - Gather event logs but log files returns empty"""
        self.set_config_values(
            'CLIENT_ID', 'CLIENT_SECRET', 'USERNAME', 'PASSWORD', 'SECURITY_TOKEN'
        )
        self._app._instance_url = 'MY_URL'

        mock_post.return_value = Mock(
            status_code=200,
            json=Mock(return_value={'access_token': 'AUTH_TOKEN', 'instance_url': 'MY_URL'})
        )

        mock_get.return_value = Mock(
            status_code=200,
            json=Mock(side_effect=[list_salesforce_api_versions(), Timeout])
        )

        assert_equal(self._app._gather_logs(), None)
        mock_logger.assert_called_once()

        mock_get.return_value = Mock(
            status_code=204,
            json=Mock(return_value={'errorCode': 'ERROR_CODE', 'message': 'error message'})
        )
        assert_equal(self._app._gather_logs(), None) 
Example #10
Source File: test_databricks.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_do_api_call_succeeds_after_retrying(self):
        for exception in [requests_exceptions.ConnectionError,
                          requests_exceptions.SSLError,
                          requests_exceptions.Timeout,
                          requests_exceptions.ConnectTimeout,
                          requests_exceptions.HTTPError]:
            with mock.patch('airflow.providers.databricks.hooks.databricks.requests') as mock_requests:
                with mock.patch.object(self.hook.log, 'error') as mock_errors:
                    setup_mock_requests(
                        mock_requests,
                        exception,
                        error_count=2,
                        response_content={'run_id': '1'}
                    )

                    response = self.hook._do_api_call(SUBMIT_RUN_ENDPOINT, {})

                    self.assertEqual(mock_errors.call_count, 2)
                    self.assertEqual(response, {'run_id': '1'}) 
Example #11
Source File: weapi.py    From netease-dl with MIT License 6 votes vote down vote up
def exception_handle(method):
    """Handle exception raised by requests library."""

    def wrapper(*args, **kwargs):
        try:
            result = method(*args, **kwargs)
            return result
        except ProxyError:
            LOG.exception('ProxyError when try to get %s.', args)
            raise ProxyError('A proxy error occurred.')
        except ConnectionException:
            LOG.exception('ConnectionError when try to get %s.', args)
            raise ConnectionException('DNS failure, refused connection, etc.')
        except Timeout:
            LOG.exception('Timeout when try to get %s', args)
            raise Timeout('The request timed out.')
        except RequestException:
            LOG.exception('RequestException when try to get %s.', args)
            raise RequestException('Please check out your network.')

    return wrapper 
Example #12
Source File: move.py    From nekoyume with MIT License 6 votes vote down vote up
def get_my_public_url():
    if 'PUBLIC_URL' in os.environ:
        return os.environ['PUBLIC_URL']
    try:
        if os.environ.get('PORT', '80') != '80':
            port = ':' + os.environ.get('PORT', '80')
        else:
            port = ''
        ip = get('http://ip.42.pl/raw').text
        has_public_address = get(
            f'http://{ip}{port}/ping'
        ).text == 'pong'
    except (ConnectionError, Timeout):
        return None
    if has_public_address:
        return f'http://{ip}{port}'
    else:
        return None 
Example #13
Source File: broadcast_test.py    From nekoyume with MIT License 6 votes vote down vote up
def test_broadcast_block_raise_exception(
        fx_session: scoped_session, fx_user: User,
        error: typing.Union[ConnectionError, Timeout]
):
    block = Block.create(fx_user, [])
    url = 'http://test.neko'
    now = datetime.datetime.utcnow()
    node = Node(url=url, last_connected_at=now)
    fx_session.add(node)
    fx_session.flush()
    with Mocker() as m:
        m.post('http://test.neko/blocks', exc=error)
        multicast(
            serialized=block.serialize(
                use_bencode=False,
                include_suffix=True,
                include_moves=True,
                include_hash=True
            ),
            broadcast=broadcast_block,
        )
        assert node.last_connected_at == now 
Example #14
Source File: queue_xqueue.py    From edx-certificates with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_length(self):
        """
        Returns the length of the queue
        """

        try:
            request = self.session.get('{0}/xqueue/get_queuelen/'.format(
                self.url), params={'queue_name': self.queue_name})
            response = json.loads(request.text)
            if response['return_code'] != 0:
                raise Exception("Invalid return code in reply")
            length = int(response['content'])
        except (ValueError, Exception, ConnectionError, Timeout) as e:
            log.critical("Unable to get queue length: {0}".format(e))
            raise

        return length 
Example #15
Source File: queue_xqueue.py    From edx-certificates with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_submission(self):
        """
        Gets a single submission from the xqueue
        server and returns the payload as a dictionary
        """

        try:
            request = self.session.get('{0}/xqueue/get_submission/'.format(
                self.url), params={'queue_name': self.queue_name})
        except (ConnectionError, Timeout) as e:
            log.critical("Unable to get submission from queue xqueue: {0}".format(e))
            raise

        try:
            response = json.loads(request.text)
            log.debug('response from get_submission: {0}'.format(response))
            if response['return_code'] != 0:
                log.critical("response: {0}".format(request.text))
                raise Exception("Invalid return code in reply")

            return json.loads(response['content'])

        except (Exception, ValueError, KeyError) as e:
            log.critical("Unable to parse xqueue message: {0} response: {1}".format(e, request.text))
            raise 
Example #16
Source File: pspider.py    From pspider with MIT License 6 votes vote down vote up
def _spider_run(self, url):
        """ 执行真正的请求。控制代理, 超时等设置。。"""
        p = None
        try_times = 0

        while True:
            try:
                if self.method == 'post':
                    resp = self.pspider.brower.post(url, timeout=self.timeout, params=self.postdata)
                elif self.method == 'get':
                    resp = self.pspider.brower.get(url, timeout=self.timeout, **self.kw)
                else:
                    raise SpiderException(SpiderException.DEFAULT, "不支持其它方法")
                log.info("请求URL={}".format(url))
                log.info("响应字段长度={}".format(len(resp.content)))
                return resp
            except (Timeout, ConnectionError):
                self.pspider.reset_brower()
                try_times += 1
                log.info("重试 ip={} url={} retry={}".format(p, url, try_times))
                if try_times >= self.retry:
                    log.info("超过重试次数 ip={} url={}".format(p, url))
                    break 
Example #17
Source File: mirrors.py    From libgen.py with MIT License 6 votes vote down vote up
def download(self, publication):
        """
        Download a publication from the mirror to the current directory.

        :param publication: a Publication
        """
        for (n, mirror) in publication.mirrors.items():
            # print(f"About to try {n}\n")
            try:
                mirror.download_publication(publication)
                break  # stop if successful
            except (CouldntFindDownloadUrl, Timeout) as e:
                print(e)
                print("Trying a different mirror.")
                continue
            except Exception:
                import traceback
                print(f"An error occurred: {sys.exc_info()[0]}")
                print(traceback.format_exc())
                print("Trying a different mirror.")
                continue
            print("Failed to download publications.") 
Example #18
Source File: coinmarketcap.py    From archon with MIT License 6 votes vote down vote up
def exchange_map():    
    endpoint_map = "exchange/map"
    parameters = {
        #'listing_status': active
    }
    #limit
    #slug
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': cmc_key,
    }

    session = Session()
    session.headers.update(headers)

    try:
        url = base_url + endpoint_map
        response = session.get(url, params=parameters)
        data = json.loads(response.text)
        return data
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e) 
Example #19
Source File: coinmarketcap.py    From archon with MIT License 6 votes vote down vote up
def get_coin_map(active):
    endpoint_map = "cryptocurrency/map"    
    parameters = {
        'listing_status': active
    }
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': cmc_key,
    }

    session = Session()
    session.headers.update(headers)

    try:
        url = base_url + endpoint_map
        response = session.get(url, params=parameters)
        data = json.loads(response.text)["data"]
        return data
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e) 
Example #20
Source File: coinmarketcap.py    From archon with MIT License 6 votes vote down vote up
def get_info(idlist):
    endpoint_description = "cryptocurrency/info"
    
    #[str(x)+',' for x in range(1,20)]
    parameters = {
        #'symbol': 'BTC,ETH,XRP,LTC'
        'id':idlist
    }
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': cmc_key,
    }

    session = Session()
    session.headers.update(headers)

    try:
        url = base_url + endpoint_description
        response = session.get(url, params=parameters)
        data = json.loads(response.text)["data"]
        return data
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print("error ", e) 
Example #21
Source File: coinmarketcap.py    From archon with MIT License 6 votes vote down vote up
def get_listings(start=1,limit=1000):
    parameters = {
        'start': start,
        'limit': str(limit),
        'convert': 'USD',
        'sort': 'market_cap'
    }
    #sort=market_cap&start=1&limit=10&cryptocurrency_type=tokens&convert=USD,BTC
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': cmc_key,
    }

    session = Session()
    session.headers.update(headers)

    endpoint_summary = 'cryptocurrency/listings/latest'
    try:
        url = base_url + endpoint_summary
        response = session.get(url, params=parameters)
        data = json.loads(response.text)["data"]
        return data
    except (ConnectionError, Timeout, TooManyRedirects) as e:
        print(e) 
Example #22
Source File: version.py    From FeelUOwn with GNU General Public License v3.0 6 votes vote down vote up
def check_release(self):
        loop = asyncio.get_event_loop()

        logger.info('正在检测更新...')
        try:
            resp = await loop.run_in_executor(
                None,
                partial(requests.get, 'https://pypi.org/pypi/feeluown/json', timeout=2)
            )
        except (ConnectionError, Timeout) as e:
            logger.warning(e)
            logger.warning('检查更新失败')
        else:
            rv = resp.json()
            latest = parse_version(rv['info']['version'])
            current = parse_version(__version__)
            if latest > current:
                msg = '检测到新版本 %s,当前版本为 %s' % (latest, current)
                logger.warning(msg)
                if self._app.mode & self._app.GuiMode:
                    self._app.ui.magicbox.show_msg(msg)
            else:
                logger.info('当前已经是最新版本')
                if self._app.mode & self._app.GuiMode:
                    self._app.ui.magicbox.show_msg('当前已经是最新版本') 
Example #23
Source File: sink.py    From scales with MIT License 6 votes vote down vote up
def _DoHttpRequestAsync(self, sink_stack, deadline, stream, msg):
    if deadline:
      timeout = deadline - time.time()
      if timeout < 0:
        sink_stack.AsyncProcessResponseMessage(MethodReturnMessage(error=TimeoutError()))
    else:
      timeout = None

    try:
      response = self._MakeRequest(msg, stream, timeout)
      if self._raise_on_http_error and 400 <= response.status_code < 600:
        err_text = 'HTTP Error %d: %s.' % (response.status_code, response.reason)
        if response.text:
          err_text += '\nThe server returned:\n%s' % response.text
        err = exceptions.HTTPError(err_text, response=response)
        msg = MethodReturnMessage(error=err)
      else:
        self._ProcessResponse(response, sink_stack)
        return
    except exceptions.Timeout:
      msg = MethodReturnMessage(error=TimeoutError())
    except Exception as ex:
      msg = MethodReturnMessage(error=ex)
    sink_stack.AsyncProcessResponseMessage(msg) 
Example #24
Source File: queue_xqueue.py    From edx-certificates with GNU Affero General Public License v3.0 6 votes vote down vote up
def _login(self):
        """
        Login to the xqueue server
        """

        try:
            self.session = requests.Session()
            self.session.auth = (self.auth_user, self.auth_pass)
            request = self.session.post('{0}/xqueue/login/'.format(self.url),
                                        data={'username': self.queue_user,
                                              'password': self.queue_pass})
            response = json.loads(request.text)
            if response['return_code'] != 0:
                raise Exception("Invalid return code in reply resp:{0}".format(
                    str(response)))
        except (Exception, ConnectionError, Timeout) as e:
            log.critical("Unable to connect to queue xqueue: {0}".format(e))
            raise 
Example #25
Source File: request.py    From FeelUOwn with GNU General Public License v3.0 5 votes vote down vote up
def post(self, *args, **kw):
        logger.info('request.post %s %s' % (args, kw))
        try:
            res = requests.post(*args, **kw)
            return res
        except ConnectionError:
            self.disconnected_signal.emit()
        except HTTPError:
            self.server_error_signal.emit()
        except Timeout:
            self.slow_signal.emit()
        return None 
Example #26
Source File: request.py    From FeelUOwn with GNU General Public License v3.0 5 votes vote down vote up
def get(self, *args, **kw):
        logger.info('request.get %s %s' % (args, kw))
        if kw.get('timeout') is None:
            kw['timeout'] = 1
        try:
            res = requests.get(*args, **kw)
            self.connected_signal.emit()
            return res
        except ConnectionError:
            self.disconnected_signal.emit()
        except HTTPError:
            self.server_error_signal.emit()
        except Timeout:
            self.slow_signal.emit()
        return None 
Example #27
Source File: connector.py    From infoblox-client with Apache License 2.0 5 votes vote down vote up
def reraise_neutron_exception(func):
    @functools.wraps(func)
    def callee(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except req_exc.Timeout as e:
            raise ib_ex.InfobloxTimeoutError(e)
        except req_exc.RequestException as e:
            raise ib_ex.InfobloxConnectionError(reason=e)

    return callee 
Example #28
Source File: PackageRequest.py    From FXTest with MIT License 5 votes vote down vote up
def delfile(self,url,params,headers):#删除的请求
        try:
            self.rdel_word=requests.delete(url,data=params,headers=headers,timeout=Interface_Time_Out)
            json_response=json.loads(self.rdel_word.text)
            spend=self.rdel_word.elapsed.total_seconds()
            return json_response,spend
        except exceptions.Timeout :
            return {'delete请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'delete请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'delete请求出错': "http请求错误"}
        except Exception as e:
            return {'delete请求出错': "错误原因:%s" % e} 
Example #29
Source File: webhelper.py    From script.artwork.beef with MIT License 5 votes vote down vote up
def __call__(self, url, **kwargs):
        try:
            return self._inner_call(url, **kwargs)
        except (Timeout, ConnectionError, RequestException) as ex:
            message = ex.response.reason if getattr(ex, 'response', None) is not None else type(ex).__name__
            raise GetterError(message, ex, not isinstance(ex, RequestException)) 
Example #30
Source File: PackageRequest.py    From FXTest with MIT License 5 votes vote down vote up
def putfile(self,url,params,headers):#put请求
        try:
            self.rdata=json.dumps(params)
            me=requests.put(url,self.rdata,headers=headers,timeout=Interface_Time_Out)
            json_response=json.loads(me.text)
            spend=me.elapsed.total_seconds()
            return json_response,spend
        except exceptions.Timeout :
            return {'put请求出错': "请求超时" }
        except exceptions.InvalidURL:
            return {'put请求出错': "非法url"}
        except exceptions.HTTPError:
            return {'put请求出错': "http请求错误"}
        except Exception as e:
            return {'put请求出错': "错误原因:%s" % e}