def connection_from_host(self, host, port=None, scheme='http'): """ Get a :class:`ConnectionPool` based on the host, port, and scheme. If ``port`` isn't given, it will be derived from the ``scheme`` using ``urllib3.connectionpool.port_by_scheme``. """ if not host: raise LocationValueError("No host specified.") scheme = scheme or 'http' port = port or port_by_scheme.get(scheme, 80) pool_key = (scheme, host, port) with self.pools.lock: # If the scheme, host, or port doesn't match existing open # connections, open a new ConnectionPool. pool = self.pools.get(pool_key) if pool: return pool # Make a fresh ConnectionPool of the desired type pool = self._new_pool(scheme, host, port) self.pools[pool_key] = pool return pool
def connection_from_host(self, host, port=None, scheme='http'): """ Get a :class:`ConnectionPool` based on the host, port, and scheme. If ``port`` isn't given, it will be derived from the ``scheme`` using ``urllib3.connectionpool.port_by_scheme``. """ if not host: raise LocationValueError("No host specified.") request_context = self.connection_pool_kw.copy() request_context['scheme'] = scheme or 'http' if not port: port = port_by_scheme.get(request_context['scheme'].lower(), 80) request_context['port'] = port request_context['host'] = host return self.connection_from_context(request_context)
def test_network_error_exception_detector(self): http_error = HTTPError() self.assertTrue(is_network_error(http_error)) location_value_error = LocationValueError() self.assertTrue(is_network_error(location_value_error)) pool_error = PoolError(None, 'an error') self.assertTrue(is_network_error(pool_error)) exception = Exception() self.assertFalse(is_network_error(exception)) w3_conn_error = Web3ConnectionException() self.assertFalse(is_network_error(w3_conn_error)) setattr(w3_conn_error, 'errno', errno.ECONNABORTED) self.assertTrue(is_network_error(w3_conn_error)) setattr(w3_conn_error, 'errno', errno.EPERM) self.assertFalse(is_network_error(w3_conn_error))
def urlopen(self, method, url, redirect=True, **kw): """ Same as :meth:`urllib3.connectionpool.HTTPConnectionPool.urlopen` with custom cross-host redirect logic and only sends the request-uri portion of the ``url``. The given ``url`` parameter must be absolute, such that an appropriate :class:`urllib3.connectionpool.ConnectionPool` can be chosen for it. """ #=============================================================================================================== # add by mz error_type = kw.get('error_type') if error_type: from urllib3.exceptions import LocationValueError, HostChangedError, LocationParseError, ConnectTimeoutError from urllib3.exceptions import ProxyError, TimeoutError, ReadTimeoutError, ProtocolError, DecodeError from urllib3.exceptions import ResponseError, ResponseNotChunked, SSLError, HTTPError, HTTPWarning, PoolError from urllib3.exceptions import RequestError, MaxRetryError, TimeoutStateError, NewConnectionError from urllib3.exceptions import EmptyPoolError, ClosedPoolError, SecurityWarning, SubjectAltNameWarning from urllib3.exceptions import InsecureRequestWarning, SystemTimeWarning, InsecurePlatformWarning from urllib3.exceptions import SNIMissingWarning, DependencyWarning, ProxySchemeUnknown, HeaderParsingError get_error = { "LocationValueError": LocationValueError(), "HostChangedError": HostChangedError(pool=1, url=2), "LocationParseError": LocationParseError(url), "ConnectTimeoutError": ConnectTimeoutError(), "ProxyError": ProxyError(), "TimeoutError": TimeoutError(), "ReadTimeoutError": ReadTimeoutError(pool=1, url=2, message="ReadTimeoutError"), "ProtocolError": ProtocolError(), "DecodeError": DecodeError(), "ResponseError": ResponseError(), "ResponseNotChunked": ResponseNotChunked(), "SSLError": SSLError(), "HTTPError": HTTPError(), "HTTPWarning": HTTPWarning(), "PoolError": PoolError(pool=1, message=2), "RequestError": RequestError(pool=1, url=2, message="RequestError"), "MaxRetryError": MaxRetryError(pool=1, url=2, reason=None), "TimeoutStateError": TimeoutStateError(), "NewConnectionError": NewConnectionError(pool=1, message="NewConnectionError"), "EmptyPoolError": EmptyPoolError(pool=1, message="EmptyPoolError"), "ClosedPoolError": ClosedPoolError(pool=1, message="ClosedPoolError"), "SecurityWarning": SecurityWarning(), "SubjectAltNameWarning": SubjectAltNameWarning(), "InsecureRequestWarning": InsecureRequestWarning(), "SystemTimeWarning": SystemTimeWarning(), "InsecurePlatformWarning": InsecurePlatformWarning(), "SNIMissingWarning": SNIMissingWarning(), "DependencyWarning": DependencyWarning(), "ProxySchemeUnknown": ProxySchemeUnknown(scheme=1), "HeaderParsingError": HeaderParsingError(defects=1, unparsed_data=2) } error_ = get_error[error_type] raise error_ #=============================================================================================================== u = parse_url(url) conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme) kw['assert_same_host'] = False kw['redirect'] = False if 'headers' not in kw: kw['headers'] = self.headers if self.proxy is not None and u.scheme == "http": response = conn.urlopen(method, url, **kw) else: response = conn.urlopen(method, u.request_uri, **kw) redirect_location = redirect and response.get_redirect_location() if not redirect_location: return response # Support relative URLs for redirecting. redirect_location = urljoin(url, redirect_location) # RFC 2616, Section 10.3.4 if response.status == 303: method = 'GET' log.info("Redirecting %s -> %s" % (url, redirect_location)) kw['retries'] = kw.get('retries', 3) - 1 # Persist retries countdown kw['redirect'] = redirect return self.urlopen(method, redirect_location, **kw)