77from socket import error as SocketError , timeout as SocketTimeout
88import socket
99
10- try : # Python 3
11- from queue import LifoQueue , Empty , Full
12- except ImportError :
13- from Queue import LifoQueue , Empty , Full
14- # Queue is imported for side effects on MS Windows
15- import Queue as _unused_module_Queue # noqa: unused
16-
1710
1811from .exceptions import (
1912 ClosedPoolError ,
3225)
3326from .packages .ssl_match_hostname import CertificateError
3427from .packages import six
28+ from .packages .six .moves .queue import LifoQueue , Empty , Full
3529from .connection import (
3630 port_by_scheme ,
3731 DummyConnection ,
4842from .util .url import get_host , Url
4943
5044
45+ if six .PY2 :
46+ # Queue is imported for side effects on MS Windows
47+ import Queue as _unused_module_Queue # noqa: F401
48+
5149xrange = six .moves .xrange
5250
5351log = logging .getLogger (__name__ )
@@ -210,8 +208,8 @@ def _new_conn(self):
210208 Return a fresh :class:`HTTPConnection`.
211209 """
212210 self .num_connections += 1
213- log .info ("Starting new HTTP connection (%d): %s" ,
214- self .num_connections , self .host )
211+ log .debug ("Starting new HTTP connection (%d): %s" ,
212+ self .num_connections , self .host )
215213
216214 conn = self .ConnectionCls (host = self .host , port = self .port ,
217215 timeout = self .timeout .connect_timeout ,
@@ -246,7 +244,7 @@ def _get_conn(self, timeout=None):
246244
247245 # If this is a persistent connection, check if it got disconnected
248246 if conn and is_connection_dropped (conn ):
249- log .info ("Resetting dropped connection: %s" , self .host )
247+ log .debug ("Resetting dropped connection: %s" , self .host )
250248 conn .close ()
251249 if getattr (conn , 'auto_open' , 1 ) == 0 :
252250 # This is a proxied connection that has been mutated by
@@ -397,8 +395,9 @@ def _make_request(self, conn, method, url, timeout=_Default, chunked=False,
397395
398396 # AppEngine doesn't have a version attr.
399397 http_version = getattr (conn , '_http_vsn_str' , 'HTTP/?' )
400- log .debug ("\" %s %s %s\" %s %s" , method , url , http_version ,
401- httplib_response .status , httplib_response .length )
398+ log .debug ("%s://%s:%s \" %s %s %s\" %s %s" , self .scheme , self .host , self .port ,
399+ method , url , http_version , httplib_response .status ,
400+ httplib_response .length )
402401
403402 try :
404403 assert_header_parsing (httplib_response .msg )
@@ -600,10 +599,14 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
600599 # mess.
601600 response_conn = conn if not release_conn else None
602601
602+ # Pass method to Response for length checking
603+ response_kw ['request_method' ] = method
604+
603605 # Import httplib's response into our own wrapper object
604606 response = self .ResponseCls .from_httplib (httplib_response ,
605607 pool = self ,
606608 connection = response_conn ,
609+ retries = retries ,
607610 ** response_kw )
608611
609612 # Everything went great!
@@ -683,7 +686,8 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
683686 raise
684687 return response
685688
686- log .info ("Redirecting %s -> %s" , url , redirect_location )
689+ retries .sleep_for_retry (response )
690+ log .debug ("Redirecting %s -> %s" , url , redirect_location )
687691 return self .urlopen (
688692 method , redirect_location , body , headers ,
689693 retries = retries , redirect = redirect ,
@@ -692,7 +696,8 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
692696 release_conn = release_conn , ** response_kw )
693697
694698 # Check if we should retry the HTTP response.
695- if retries .is_forced_retry (method , status_code = response .status ):
699+ has_retry_after = bool (response .getheader ('Retry-After' ))
700+ if retries .is_retry (method , response .status , has_retry_after ):
696701 try :
697702 retries = retries .increment (method , url , response = response , _pool = self )
698703 except MaxRetryError :
@@ -702,8 +707,8 @@ def urlopen(self, method, url, body=None, headers=None, retries=None,
702707 response .release_conn ()
703708 raise
704709 return response
705- retries .sleep ()
706- log .info ( "Forced retry : %s" , url )
710+ retries .sleep (response )
711+ log .debug ( "Retry : %s" , url )
707712 return self .urlopen (
708713 method , url , body , headers ,
709714 retries = retries , redirect = redirect ,
@@ -775,7 +780,6 @@ def _prepare_conn(self, conn):
775780 assert_hostname = self .assert_hostname ,
776781 assert_fingerprint = self .assert_fingerprint )
777782 conn .ssl_version = self .ssl_version
778-
779783 return conn
780784
781785 def _prepare_proxy (self , conn ):
@@ -801,8 +805,8 @@ def _new_conn(self):
801805 Return a fresh :class:`httplib.HTTPSConnection`.
802806 """
803807 self .num_connections += 1
804- log .info ("Starting new HTTPS connection (%d): %s" ,
805- self .num_connections , self .host )
808+ log .debug ("Starting new HTTPS connection (%d): %s" ,
809+ self .num_connections , self .host )
806810
807811 if not self .ConnectionCls or self .ConnectionCls is DummyConnection :
808812 raise SSLError ("Can't connect to HTTPS URL because the SSL "
@@ -834,7 +838,8 @@ def _validate_conn(self, conn):
834838 warnings .warn ((
835839 'Unverified HTTPS request is being made. '
836840 'Adding certificate verification is strongly advised. See: '
837- 'https://urllib3.readthedocs.io/en/latest/security.html' ),
841+ 'https://urllib3.readthedocs.io/en/latest/advanced-usage.html'
842+ '#ssl-warnings' ),
838843 InsecureRequestWarning )
839844
840845
0 commit comments