Python requests.exceptions.ConnectTimeout() Examples

The following are 30 code examples of requests.exceptions.ConnectTimeout(). 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: 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 #2
Source File: Struts2Scan.py    From Struts2-Scan with GNU General Public License v3.0 6 votes vote down vote up
def get_302(url, headers=None, encoding='UTF-8'):
    """GET请求发送包装"""
    try:
        html = requests.get(url, headers=headers, proxies=proxies, timeout=_tiemout, allow_redirects=False)
        status_code = html.status_code
        if status_code == 302:
            html = html.headers.get("Location", "")
        elif status_code == 200:
            html = html.content.decode(encoding)
            html = html.replace('\x00', '').strip()
        else:
            html = ""
        return html
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #3
Source File: test_http.py    From integrations-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_no_proxy_domain_fail(self, socks5_proxy):
        instance = {'proxy': {'http': 'http://1.2.3.4:567', 'no_proxy': '.google.com,example.com,example,9'}}
        init_config = {}
        http = RequestsWrapper(instance, init_config)

        # no_proxy not match: .google.com
        # ".y.com" matches "x.y.com" but not "y.com"
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://google.com', timeout=1)

        # no_proxy not match: example or example.com
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://notexample.com', timeout=1)

        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://example.org', timeout=1)

        # no_proxy not match: 9
        with pytest.raises((ConnectTimeout, ProxyError)):
            http.get('http://127.0.0.99', timeout=1) 
Example #4
Source File: http_client.py    From SmartQQBot with GNU General Public License v3.0 6 votes vote down vote up
def post(self, url, data, refer=None):
        try:
            resp = self.session.post(
                url,
                data,
                headers=self._get_headers({'Referer': refer or SMART_QQ_REFER}),
                verify=SSL_VERIFY,
            )
        except requests.exceptions.SSLError:
            logger.exception("SSL连接验证失败,请检查您所在的网络环境。如果需要禁用SSL验证,请修改config.py中的SSL_VERIFY为False")
        except (excps.ConnectTimeout, excps.HTTPError):
            error_msg = "Failed to send request to `{0}`".format(
                url
            )
            logger.exception(error_msg)
            return error_msg
        else:
            self._cookies.save(COOKIE_FILE, ignore_discard=True, ignore_expires=True)
            return resp.text 
Example #5
Source File: http_client.py    From SmartQQBot with GNU General Public License v3.0 6 votes vote down vote up
def get(self, url, refer=None):
        try:
            resp = self.session.get(
                url,
                headers=self._get_headers({'Referer': refer or SMART_QQ_REFER}),
                verify=SSL_VERIFY,
            )
        except (excps.ConnectTimeout, excps.HTTPError):
            error_msg = "Failed to send finish request to `{0}`".format(
                url
            )
            logger.exception(error_msg)
            return error_msg
        except requests.exceptions.SSLError:
            logger.exception("SSL连接验证失败,请检查您所在的网络环境。如果需要禁用SSL验证,请修改config.py中的SSL_VERIFY为False")
        else:
            self._cookies.save(COOKIE_FILE, ignore_discard=True, ignore_expires=True)
            return resp.text 
Example #6
Source File: test_client.py    From storops with Apache License 2.0 6 votes vote down vote up
def test_cs_request_connect_timeout(self, mocked_wait_callback):
        mocked_wait_callback.return_value = 0

        self.client.session.request = mock.MagicMock(
            side_effect=exceptions.ConnectTimeout)

        self.client.base_url = 'https://10.10.10.10'
        assert_that(calling(self.client._cs_request).with_args(
            '/api/types/instance', 'GET'), raises(StoropsConnectTimeoutError))

        calls = [
            mock.call('GET', 'https://10.10.10.10/api/types/instance',
                      auth=None, files=None, verify=True, headers={}),
            mock.call('GET', 'https://10.10.10.10/api/types/instance',
                      auth=None, files=None, verify=True, headers={}),
        ]
        self.client.session.request.assert_has_calls(calls) 
Example #7
Source File: tensor_serving_checker.py    From grinder with GNU General Public License v2.0 6 votes vote down vote up
def main(host_info: dict, timeout: int = 3) -> dict:
    """
    Check if host is TensorFlow Serving Model
    :param host_info: host information
    :param timeout: host timeout
    :return: dictionary with status and data
    """
    output = {"html": "", "status": ""}

    try:
        url = f"http://{host_info.get('ip')}:{host_info.get('port')}/v1/models"
        resp = get(url, verify=False, timeout=timeout).text
    except (TimeoutError, ConnectionError, ConnectTimeout, ContentDecodingError):
        output.update({"status": "Timeout Error Was Caught"})
        return output
    if "Missing model name in request" in resp:
        status = "Found TensorFlow Serving Server"
    elif "404" not in resp:
        status = "Possibly TensorFlow Serving Server"
    else:
        status = "Not TensorFlow Serving Server"
    output.update({"html": resp, "status": status})
    return output 
Example #8
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 #9
Source File: struts2scan_api.py    From ParrotSecCN_Community_QQbot with GNU General Public License v2.0 6 votes vote down vote up
def post_stream(url, data=None, headers=None, encoding='UTF-8', files=None):
    """分块接受数据"""
    try:
        lines = requests.post(url, data=data, headers=headers, timeout=_tiemout, stream=True, proxies=proxies,
                              files=None)
        html = list()
        for line in lines.iter_lines():
            line = line.decode(encoding)
            html.append(line.strip())
        return '\r\n'.join(html).strip()
    except ChunkedEncodingError as e:
        return '\r\n'.join(html).strip()
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #10
Source File: struts2scan_api.py    From ParrotSecCN_Community_QQbot with GNU General Public License v2.0 6 votes vote down vote up
def get_stream(url, headers=None, encoding='UTF-8'):
    """分块接受数据"""
    try:
        lines = requests.get(url, headers=headers, timeout=_tiemout, stream=True, proxies=proxies)
        html = list()
        for line in lines.iter_lines():
            if b'\x00' in line:
                break
            line = line.decode(encoding)
            html.append(line.strip())
        return '\r\n'.join(html).strip()
    except ChunkedEncodingError as e:
        return '\r\n'.join(html).strip()
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #11
Source File: struts2scan_api.py    From ParrotSecCN_Community_QQbot with GNU General Public License v2.0 6 votes vote down vote up
def get_302(url, headers=None, encoding='UTF-8'):
    """GET请求发送包装"""
    try:
        html = requests.get(url, headers=headers, proxies=proxies, timeout=_tiemout, allow_redirects=False)
        status_code = html.status_code
        if status_code == 302:
            html = html.headers.get("Location", "")
        elif status_code == 200:
            html = html.content.decode(encoding)
            html = html.replace('\x00', '').strip()
        else:
            html = ""
        return html
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #12
Source File: Struts2Scan.py    From Struts2-Scan with GNU General Public License v3.0 6 votes vote down vote up
def post_stream(url, data=None, headers=None, encoding='UTF-8', files=None):
    """分块接受数据"""
    try:
        lines = requests.post(url, data=data, headers=headers, timeout=_tiemout, stream=True, proxies=proxies,
                              files=None)
        html = list()
        for line in lines.iter_lines():
            line = line.decode(encoding)
            html.append(line.strip())
        return '\r\n'.join(html).strip()
    except ChunkedEncodingError as e:
        return '\r\n'.join(html).strip()
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #13
Source File: views.py    From edx-analytics-dashboard with GNU Affero General Public License v3.0 5 votes vote down vote up
def handle_exception(self, exc):
        """
        Handles timeouts raised by the API client by returning an HTTP
        504.
        """
        if isinstance(exc, ConnectTimeout):
            return Response(
                data={'developer_message': 'Learner Analytics API timed out.', 'error_code': 'analytics_api_timeout'},
                status=504
            )
        return super(BaseLearnerApiView, self).handle_exception(exc) 
Example #14
Source File: test_api.py    From pypac with Apache License 2.0 5 votes vote down vote up
def test_timeout_proxy(self):
        # Travis can refuse quickly, and trigger ProxyError instead.
        session = requests.Session()
        with pytest.raises(ConnectTimeout):
            session.get(arbitrary_url, timeout=0.001, proxies=proxy_parameter_for_requests('http://localhost')) 
Example #15
Source File: request_handler.py    From Raccoon with MIT License 5 votes vote down vote up
def send(self, method="GET", *args, **kwargs):
        """
        Send a GET/POST/HEAD request using the object's proxies and headers
        :param method: Method to send request in. GET/POST/HEAD
        """
        proxies = self._get_request_proxies()

        try:
            if method.upper() in self.allowed_methods:
                kwargs['timeout'] = kwargs['timeout'] if 'timeout' in kwargs else 5
                return request(method, proxies=proxies, headers=self.headers, cookies=self.cookies, *args, **kwargs)
            else:
                raise RequestHandlerException("Unsupported method: {}".format(method))
        except ProxyError:
            # TODO: Apply fail over for bad proxies or drop them
            raise RequestHandlerException("Error connecting to proxy")
        except (ConnectTimeout, ReadTimeout):
            raise RequestHandlerException("Connection with server timed out")
        except NewConnectionError:
            raise RequestHandlerException("Address cannot be resolved")
            # New connection error == Can't resolve address
        except ConnectionError:
            # TODO: Increase delay
            raise RequestHandlerException("Error connecting to host")
        except TooManyRedirects:
            raise RequestHandlerException("Infinite redirects detected - too many redirects error")
        except UnicodeDecodeError:
            # Following issue #19, apparently some sites do not use utf-8 in their uris :<>
            pass 
Example #16
Source File: api.py    From pypac with Apache License 2.0 5 votes vote down vote up
def default_proxy_fail_exception_filter(req_exc):
    return isinstance(req_exc, (ProxyError, ConnectTimeout)) 
Example #17
Source File: struts2scan_api.py    From ParrotSecCN_Community_QQbot with GNU General Public License v2.0 5 votes vote down vote up
def post(url, data=None, headers=None, encoding='UTF-8', files=None):
    """POST请求发送包装"""
    try:
        html = requests.post(url, data=data, headers=headers, proxies=proxies, timeout=_tiemout, files=files)
        html = html.content.decode(encoding)
        return html.replace('\x00', '').strip()
    except ChunkedEncodingError as e:
        html = post_stream(url, data, headers, encoding, files)
        return html
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #18
Source File: test_http_checks.py    From syntribos with Apache License 2.0 5 votes vote down vote up
def test_connect_timeout(self):
        signal = http_checks.check_fail(rex.ConnectTimeout())
        self._assert_has_tags(self.conn_fail_tags, signal)
        self._assert_has_slug("HTTP_FAIL_CONNECT_TIMEOUT", signal) 
Example #19
Source File: test_client.py    From storops with Apache License 2.0 5 votes vote down vote up
def test_request_when_connect_timeout(self):
        self.client.session.request = mock.MagicMock(
            side_effect=exceptions.ConnectTimeout)

        def _tmp_func():
            self.client.request('fake_url_connect_timeout', 'GET',
                                body='{"k_abc": "v_abc"}')
        assert_that(calling(_tmp_func),
                    raises(exceptions.ConnectTimeout))

        self.client.session.request.assert_called_with(
            'GET', 'fake_url_connect_timeout', auth=None, files=None,
            verify=True, headers={}, data='{"k_abc": "v_abc"}') 
Example #20
Source File: client.py    From storops with Apache License 2.0 5 votes vote down vote up
def _cs_request(self, url, method, **kwargs):
        try:
            return self._cs_request_with_retries(
                self.base_url + url,
                method,
                **kwargs)
        except ConnectTimeout as ex:
            raise StoropsConnectTimeoutError(message=str(ex)) 
Example #21
Source File: code_all_articles.py    From chicago-justice with GNU General Public License v3.0 5 votes vote down vote up
def get(self, url, params=None, retries=3):
        try:
            return requests.get(url,
                headers=self.headers(),
                params=params,
                timeout=ApiClient.TIMEOUT_S)
        except (ReadTimeout, ConnectTimeout):
            if retries >= 1:
                print("retrying GET {}".format(url))
                return self.get(url, params=params, retries=retries-1) 
Example #22
Source File: code_all_articles.py    From chicago-justice with GNU General Public License v3.0 5 votes vote down vote up
def put(self, url, json=None, params=None, retries=3):
        try:
            return requests.put(url,
                headers=self.headers(),
                params=params,
                json=json,
                timeout=ApiClient.TIMEOUT_S)
        except (ReadTimeout, ConnectTimeout):
            if retries >= 1:
                print("retrying PUT {}".format(url))
                return self.put(url, json=json, params=params, retries=retries-1) 
Example #23
Source File: client.py    From patzilla with GNU Affero General Public License v3.0 5 votes vote down vote up
def login(self):
        starttime = timeit.default_timer()

        try:
            response = requests.post(self.uri + '/login', data={'Username': self.username, 'Password': self.password}, timeout=(3, 30))

        except (ConnectionError, ConnectTimeout) as ex:
            log.error('SIP login for user "{username}" at "{uri}" failed. Reason: {0} {1}.'.format(
                ex.__class__, ex.message, username=self.username, uri=self.uri))
            self.logout()
            error = LoginException(ex.message)
            error.sip_info = 'Error or timeout while connecting to upstream database. Database might be offline.'
            raise error

        if response.status_code == 200:
            try:
                self.sessionid = self._login_parse_xml(response.content)
                duration = timeit.default_timer() - starttime
                log.info('SIP login succeeded. sessionid={0}, duration={1}s'.format(self.sessionid, round(duration, 1)))
                return True
            except Exception as ex:
                log.error('SIP login for user "{username}" failed. Reason: {0} {1}. status_code={2}, response={3}'.format(
                    ex.__class__, ex.message, response.status_code, response.content, username=self.username))
                self.logout()
                raise
        else:
            message = 'SIP login failed. status_code={0}, content={1}'.format(response.status_code, response.content)
            log.error(message)
            error = LoginException(message)
            error.sip_info = 'Login to upstream database failed.'
            self.logout()
            raise error

        self.sessionid = None
        return False 
Example #24
Source File: http_status.py    From grinder with GNU General Public License v2.0 5 votes vote down vote up
def main(host_info: dict, timeout: int = 3) -> dict:
    """
    Check HTTP status
    :param host_info: host information
    :param timeout: host timeout
    :return: dictionary with status and data
    """
    output = {"http_status": "", "status": "Success"}

    try:
        proto = "https" if host_info.get("port") in [443, 8443] else "http"
        url = f"{proto}://{host_info.get('ip')}:{host_info.get('port')}"
        status_code = get(url, verify=False, timeout=timeout).status_code
    except (TimeoutError, ConnectTimeout):
        output.update({"status": "Timeout error was caught"})
    except ConnectionError as connection_err:
        output.update({"status": f"Connection error was caught: {str(connection_err)}"})
    except ContentDecodingError as content_err:
        output.update(
            {"status": f"Content decoding error was caight: {str(content_err)}"}
        )
    except Exception as unexp_err:
        output.update({"status": f"Unexpected error was caught: {str(unexp_err)}"})
    else:
        output.update({"http_status": status_code})
    return output 
Example #25
Source File: Struts2Scan.py    From Struts2-Scan with GNU General Public License v3.0 5 votes vote down vote up
def post(url, data=None, headers=None, encoding='UTF-8', files=None):
    """POST请求发送包装"""
    try:
        html = requests.post(url, data=data, headers=headers, proxies=proxies, timeout=_tiemout, files=files)
        html = html.content.decode(encoding)
        return html.replace('\x00', '').strip()
    except ChunkedEncodingError as e:
        html = post_stream(url, data, headers, encoding, files)
        return html
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #26
Source File: Struts2Scan.py    From Struts2-Scan with GNU General Public License v3.0 5 votes vote down vote up
def get_stream(url, headers=None, encoding='UTF-8'):
    """分块接受数据"""
    try:
        lines = requests.get(url, headers=headers, timeout=_tiemout, stream=True, proxies=proxies)
        html = list()
        for line in lines.iter_lines():
            if b'\x00' in line:
                break
            line = line.decode(encoding)
            html.append(line.strip())
        return '\r\n'.join(html).strip()
    except ChunkedEncodingError as e:
        return '\r\n'.join(html).strip()
    except ConnectionError as e:
        return "ERROR:" + "HTTP连接错误"
    except ConnectTimeout as e:
        return "ERROR:" + "HTTP连接超时错误"
    except Exception as e:
        return 'ERROR:' + str(e) 
Example #27
Source File: http_client.py    From SmartQQBot with GNU General Public License v3.0 5 votes vote down vote up
def download(self, url, fname):
        with open(fname, "wb") as o_file:
            try:
                resp = self.session.get(url, stream=True, verify=SSL_VERIFY)
            except requests.exceptions.SSLError:
                logger.exception("SSL连接验证失败,请检查您所在的网络环境。如果需要禁用SSL验证,请修改config.py中的SSL_VERIFY为False")
            except (excps.ConnectTimeout, excps.HTTPError):
                error_msg = "Failed to send request to `{0}`".format(
                    url
                )
                logger.exception(error_msg)
                return error_msg
            else:
                self._cookies.save(COOKIE_FILE, ignore_discard=True, ignore_expires=True)
                o_file.write(resp.raw.read()) 
Example #28
Source File: utils.py    From ecommerce with GNU Affero General Public License v3.0 5 votes vote down vote up
def user_already_placed_order(user, product, site):
        """
        Checks if the user has already purchased the product.

        A product is considered purchased if an OrderLine exists for the product,
        and it has not been refunded.

        Args:
            user: (User)
            product: (Product)

        Returns:
            bool: True if user has purchased the product.

        Notes:
            If the switch with the name `ecommerce.extensions.order.constants.DISABLE_REPEAT_ORDER_SWITCH_NAME`
            is active this check will be disabled, and this method will already return `False`.
        """
        if waffle.switch_is_active(DISABLE_REPEAT_ORDER_CHECK_SWITCH_NAME):
            return False

        entitlement_option = Option.objects.get(code='course_entitlement')

        orders_lines = OrderLine.objects.filter(product=product, order__user=user)
        for order_line in orders_lines:
            if not UserAlreadyPlacedOrder.is_order_line_refunded(order_line):
                if not order_line.product.is_course_entitlement_product:
                    return True

                entitlement_uuid = order_line.attributes.get(option=entitlement_option).value
                try:
                    if not UserAlreadyPlacedOrder.is_entitlement_expired(entitlement_uuid, site):
                        return True
                except (ConnectTimeout, ReqConnectionError, HttpNotFoundError):
                    logger.exception(
                        'Unable to get entitlement info [%s] due to a network problem',
                        entitlement_uuid
                    )

        return False 
Example #29
Source File: test_client.py    From hdfs with MIT License 5 votes vote down vote up
def test_timeout(self):
    self.client._timeout = 1e-4 # Small enough for it to always timeout.
    try:
      self.client.status('.')
    except (ConnectTimeout, ReadTimeout):
      self.client._timeout = None
    else:
      raise HdfsError('No timeout.') 
Example #30
Source File: test_client.py    From correios with Apache License 2.0 5 votes vote down vote up
def test_client_timeout_error(mock_soap_client, client):
    mock_soap_client.side_effect = ConnectTimeout()
    with pytest.raises(ConnectTimeoutError):
        client.find_zipcode(ZipCode("70002-900"))