我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用urllib3.exceptions.ConnectTimeoutError()。
def get_resource(self, resource, namespace="all"): ret, resources = None, list() try: ret, namespaced_resource = self._call_api_client(resource) except ApiException as ae: self.logger.warning("resource autocomplete disabled, encountered " "ApiException", exc_info=1) except (NewConnectionError, MaxRetryError, ConnectTimeoutError): self.logger.warning("unable to connect to k8 cluster", exc_info=1) if ret: for i in ret.items: if namespace == "all" or not namespaced_resource: resources.append((i.metadata.name, i.metadata.namespace)) elif namespace == i.metadata.namespace: resources.append((i.metadata.name, i.metadata.namespace)) return resources
def request(self, url): """ Client request HTTP :param str url: request uri :return: urllib3.HTTPResponse """ if self._HTTP_DBG_LEVEL <= self.__debug.level: self.__debug.debug_request(self._headers, url, self.__cfg.method) try: if self.__cfg.DEFAULT_SCAN == self.__cfg.scan: response = self.__pool.request(self.__cfg.method, helper.parse_url(url).path, headers=self._headers, retries=self.__cfg.retries, assert_same_host=True, redirect=False) self.cookies_middleware(is_accept=self.__cfg.accept_cookies, response=response) else: response = PoolManager().request(self.__cfg.method, url, headers=self._headers, retries=self.__cfg.retries, assert_same_host=False, redirect=False) return response except MaxRetryError: if self.__cfg.DEFAULT_SCAN == self.__cfg.scan: self.__tpl.warning(key='max_retry_error', url=helper.parse_url(url).path) pass except HostChangedError as error: self.__tpl.warning(key='host_changed_error', details=error) pass except ReadTimeoutError: self.__tpl.warning(key='read_timeout_error', url=url) pass except ConnectTimeoutError: self.__tpl.warning(key='connection_timeout_error', url=url) pass
def _new_conn(self): """ Establish a new connection via the SOCKS proxy. """ extra_kw = {} if self.source_address: extra_kw['source_address'] = self.source_address if self.socket_options: extra_kw['socket_options'] = self.socket_options try: conn = socks.create_connection( (self.host, self.port), proxy_type=self._socks_options['socks_version'], proxy_addr=self._socks_options['proxy_host'], proxy_port=self._socks_options['proxy_port'], proxy_username=self._socks_options['username'], proxy_password=self._socks_options['password'], timeout=self.timeout, **extra_kw ) except SocketTimeout as e: raise ConnectTimeoutError( self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout)) except socks.ProxyError as e: # This is fragile as hell, but it seems to be the only way to raise # useful errors here. if e.socket_err: error = e.socket_err if isinstance(error, SocketTimeout): raise ConnectTimeoutError( self, "Connection to %s timed out. (connect timeout=%s)" % (self.host, self.timeout) ) else: raise NewConnectionError( self, "Failed to establish a new connection: %s" % error ) else: raise NewConnectionError( self, "Failed to establish a new connection: %s" % e ) except SocketError as e: # Defensive: PySocks should catch all these. raise NewConnectionError( self, "Failed to establish a new connection: %s" % e) return conn # We don't need to duplicate the Verified/Unverified distinction from # urllib3/connection.py here because the HTTPSConnection will already have been # correctly set to either the Verified or Unverified form by that module. This # means the SOCKSHTTPSConnection will automatically be the correct type.