38
41
from .response import HTTPResponse
40
43
from .util.connection import is_connection_dropped
44
from .util.response import assert_header_parsing
41
45
from .util.retry import Retry
42
46
from .util.timeout import Timeout
43
from .util.url import get_host
47
from .util.url import get_host, Url
46
50
xrange = six.moves.xrange
72
76
return '%s(host=%r, port=%r)' % (type(self).__name__,
73
77
self.host, self.port)
82
def __exit__(self, exc_type, exc_val, exc_tb):
84
# Return False to re-raise any potential exceptions
89
Close all pooled connections and disable the pool.
75
94
# This is taken from http://hg.python.org/cpython/file/7aaba721ebc0/Lib/socket.py#l252
76
95
_blocking_errnos = set([errno.EAGAIN, errno.EWOULDBLOCK])
107
126
Number of connections to save that can be reused. More than 1 is useful
108
in multithreaded situations. If ``block`` is set to false, more
127
in multithreaded situations. If ``block`` is set to False, more
109
128
connections will be created but they will not be saved once they've
288
def _prepare_proxy(self, conn):
289
# Nothing to do for HTTP connections.
269
292
def _get_timeout(self, timeout):
270
293
""" Helper that always returns a :class:`urllib3.util.Timeout` """
271
294
if timeout is _Default:
350
373
# Receive the response from the server
352
try: # Python 2.7+, use buffering of HTTP responses
375
try: # Python 2.7, use buffering of HTTP responses
353
376
httplib_response = conn.getresponse(buffering=True)
354
377
except TypeError: # Python 2.6 and older
355
378
httplib_response = conn.getresponse()
362
385
log.debug("\"%s %s %s\" %s %s" % (method, url, http_version,
363
386
httplib_response.status,
364
387
httplib_response.length))
390
assert_header_parsing(httplib_response.msg)
391
except HeaderParsingError as hpe: # Platform-specific: Python 3
393
'Failed to parse headers (url=%s): %s',
394
self._absolute_url(url), hpe, exc_info=True)
365
396
return httplib_response
398
def _absolute_url(self, path):
399
return Url(scheme=self.scheme, host=self.host, port=self.port, path=path).url
369
403
Close all pooled connections and disable the pool.
512
546
# Request a connection from the queue.
547
timeout_obj = self._get_timeout(timeout)
513
548
conn = self._get_conn(timeout=pool_timeout)
550
conn.timeout = timeout_obj.connect_timeout
552
is_new_proxy_conn = self.proxy is not None and not getattr(conn, 'sock', None)
553
if is_new_proxy_conn:
554
self._prepare_proxy(conn)
515
556
# Make the request on the httplib connection object.
516
557
httplib_response = self._make_request(conn, method, url,
518
559
body=body, headers=headers)
520
561
# If we're going to release the connection in ``finally:``, then
542
583
# Close the connection. If a connection is reused on which there
543
584
# was a Certificate error, the next request will certainly raise
544
585
# another Certificate error.
586
conn = conn and conn.close()
548
588
raise SSLError(e)
550
except (TimeoutError, HTTPException, SocketError, ConnectionError) as e:
552
# Discard the connection for these exceptions. It will be
553
# be replaced during the next _get_conn() call.
557
stacktrace = sys.exc_info()[2]
558
if isinstance(e, SocketError) and self.proxy:
591
# Treat SSLError separately from BaseSSLError to preserve
593
conn = conn and conn.close()
597
except (TimeoutError, HTTPException, SocketError, ProtocolError) as e:
598
# Discard the connection for these exceptions. It will be
599
# be replaced during the next _get_conn() call.
600
conn = conn and conn.close()
603
if isinstance(e, (SocketError, NewConnectionError)) and self.proxy:
559
604
e = ProxyError('Cannot connect to proxy.', e)
560
605
elif isinstance(e, (SocketError, HTTPException)):
561
606
e = ProtocolError('Connection aborted.', e)
563
retries = retries.increment(method, url, error=e,
564
_pool=self, _stacktrace=stacktrace)
608
retries = retries.increment(method, url, error=e, _pool=self,
609
_stacktrace=sys.exc_info()[2])
567
612
# Keep track of the error for the retry warning.
593
638
retries = retries.increment(method, url, response=response, _pool=self)
594
639
except MaxRetryError:
595
640
if retries.raise_on_redirect:
641
# Release the connection for this response, since we're not
642
# returning it to be released manually.
643
response.release_conn()
629
677
``assert_hostname`` and ``host`` in this order to verify connections.
630
678
If ``assert_hostname`` is False, no verification is done.
632
The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs`` and
633
``ssl_version`` are only used if :mod:`ssl` is available and are fed into
634
:meth:`urllib3.util.ssl_wrap_socket` to upgrade the connection socket
680
The ``key_file``, ``cert_file``, ``cert_reqs``, ``ca_certs``,
681
``ca_cert_dir``, and ``ssl_version`` are only used if :mod:`ssl` is
682
available and are fed into :meth:`urllib3.util.ssl_wrap_socket` to upgrade
683
the connection socket into an SSL socket.
645
693
key_file=None, cert_file=None, cert_reqs=None,
646
694
ca_certs=None, ssl_version=None,
647
695
assert_hostname=None, assert_fingerprint=None,
696
ca_cert_dir=None, **conn_kw):
650
698
HTTPConnectionPool.__init__(self, host, port, strict, timeout, maxsize,
651
699
block, headers, retries, _proxy, _proxy_headers,
702
if ca_certs and cert_reqs is None:
703
cert_reqs = 'CERT_REQUIRED'
653
705
self.key_file = key_file
654
706
self.cert_file = cert_file
655
707
self.cert_reqs = cert_reqs
656
708
self.ca_certs = ca_certs
709
self.ca_cert_dir = ca_cert_dir
657
710
self.ssl_version = ssl_version
658
711
self.assert_hostname = assert_hostname
659
712
self.assert_fingerprint = assert_fingerprint
669
722
cert_file=self.cert_file,
670
723
cert_reqs=self.cert_reqs,
671
724
ca_certs=self.ca_certs,
725
ca_cert_dir=self.ca_cert_dir,
672
726
assert_hostname=self.assert_hostname,
673
727
assert_fingerprint=self.assert_fingerprint)
674
728
conn.ssl_version = self.ssl_version
676
if self.proxy is not None:
679
set_tunnel = conn.set_tunnel
680
except AttributeError: # Platform-specific: Python 2.6
681
set_tunnel = conn._set_tunnel
683
if sys.version_info <= (2, 6, 4) and not self.proxy_headers: # Python 2.6.4 and older
684
set_tunnel(self.host, self.port)
686
set_tunnel(self.host, self.port, self.proxy_headers)
688
# Establish tunnel connection early, because otherwise httplib
689
# would improperly set Host: header to proxy's IP:port.
732
def _prepare_proxy(self, conn):
734
Establish tunnel connection early, because otherwise httplib
735
would improperly set Host: header to proxy's IP:port.
739
set_tunnel = conn.set_tunnel
740
except AttributeError: # Platform-specific: Python 2.6
741
set_tunnel = conn._set_tunnel
743
if sys.version_info <= (2, 6, 4) and not self.proxy_headers: # Python 2.6.4 and older
744
set_tunnel(self.host, self.port)
746
set_tunnel(self.host, self.port, self.proxy_headers)
694
750
def _new_conn(self):
696
752
Return a fresh :class:`httplib.HTTPSConnection`.
700
756
% (self.num_connections, self.host))
702
758
if not self.ConnectionCls or self.ConnectionCls is DummyConnection:
703
# Platform-specific: Python without ssl
704
759
raise SSLError("Can't connect to HTTPS URL because the SSL "
705
760
"module is not available.")