<s> import errno <EOL> import logging <EOL> from socket import error as SocketError , timeout as SocketTimeout <EOL> import socket <EOL> try : <EOL> from queue import LifoQueue , Empty , Full <EOL> except ImportError : <EOL> from Queue import LifoQueue , Empty , Full <EOL> import Queue as _ <EOL> from . exceptions import ( <EOL> ClosedPoolError , <EOL> ConnectTimeoutError , <EOL> EmptyPoolError , <EOL> HostChangedError , <EOL> MaxRetryError , <EOL> SSLError , <EOL> TimeoutError , <EOL> ReadTimeoutError , <EOL> ProxyError , <EOL> ) <EOL> from . packages . ssl_match_hostname import CertificateError <EOL> from . packages import six <EOL> from . connection import ( <EOL> DummyConnection , <EOL> HTTPConnection , HTTPSConnection , VerifiedHTTPSConnection , <EOL> HTTPException , BaseSSLError , <EOL> ) <EOL> from . request import RequestMethods <EOL> from . response import HTTPResponse <EOL> from . util import ( <EOL> assert_fingerprint , <EOL> get_host , <EOL> is_connection_dropped , <EOL> Timeout , <EOL> ) <EOL> xrange = six . moves . xrange <EOL> log = logging . getLogger ( __name__ ) <EOL> _Default = object ( ) <EOL> port_by_scheme = { <EOL> '<STR_LIT:http>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> class ConnectionPool ( object ) : <EOL> """<STR_LIT>""" <EOL> scheme = None <EOL> QueueCls = LifoQueue <EOL> def __init__ ( self , host , port = None ) : <EOL> host = host . strip ( '<STR_LIT>' ) <EOL> self . host = host <EOL> self . port = port <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( type ( self ) . __name__ , <EOL> self . host , self . port ) <EOL> _blocking_errnos = set ( [ errno . EAGAIN , errno . EWOULDBLOCK ] ) <EOL> class HTTPConnectionPool ( ConnectionPool , RequestMethods ) : <EOL> """<STR_LIT>""" <EOL> scheme = '<STR_LIT:http>' <EOL> ConnectionCls = HTTPConnection <EOL> def __init__ ( self , host , port = None , strict = False , <EOL> timeout = Timeout . DEFAULT_TIMEOUT , maxsize = <NUM_LIT:1> , block = False , <EOL> headers = None , _proxy = None , _proxy_headers = None ) : <EOL> ConnectionPool . __init__ ( self , host , port ) <EOL> RequestMethods . __init__ ( self , headers ) <EOL> self . strict = strict <EOL> if not isinstance ( timeout , Timeout ) : <EOL> timeout = Timeout . from_float ( timeout ) <EOL> self . timeout = timeout <EOL> self . pool = self . QueueCls ( maxsize ) <EOL> self . block = block <EOL> self . proxy = _proxy <EOL> self . proxy_headers = _proxy_headers or { } <EOL> for _ in xrange ( maxsize ) : <EOL> self . pool . put ( None ) <EOL> self . num_connections = <NUM_LIT:0> <EOL> self . num_requests = <NUM_LIT:0> <EOL> def _new_conn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . num_connections += <NUM_LIT:1> <EOL> log . info ( "<STR_LIT>" % <EOL> ( self . num_connections , self . host ) ) <EOL> extra_params = { } <EOL> if not six . PY3 : <EOL> extra_params [ '<STR_LIT:strict>' ] = self . strict <EOL> return self . ConnectionCls ( host = self . host , port = self . port , <EOL> timeout = self . timeout . connect_timeout , <EOL> ** extra_params ) <EOL> def _get_conn ( self , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> conn = None <EOL> try : <EOL> conn = self . pool . get ( block = self . block , timeout = timeout ) <EOL> except AttributeError : <EOL> raise ClosedPoolError ( self , "<STR_LIT>" ) <EOL> except Empty : <EOL> if self . block : <EOL> raise EmptyPoolError ( self , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> pass <EOL> if conn and is_connection_dropped ( conn ) : <EOL> log . info ( "<STR_LIT>" % self . host ) <EOL> conn . close ( ) <EOL> return conn or self . _new_conn ( ) <EOL> def _put_conn ( self , conn ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . pool . put ( conn , block = False ) <EOL> return <EOL> except AttributeError : <EOL> pass <EOL> except Full : <EOL> log . warning ( "<STR_LIT>" <EOL> % self . host ) <EOL> if conn : <EOL> conn . close ( ) <EOL> def _get_timeout ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is _Default : <EOL> return self . timeout . clone ( ) <EOL> if isinstance ( timeout , Timeout ) : <EOL> return timeout . clone ( ) <EOL> else : <EOL> return Timeout . from_float ( timeout ) <EOL> def _make_request ( self , conn , method , url , timeout = _Default , <EOL> ** httplib_request_kw ) : <EOL> """<STR_LIT>""" <EOL> self . num_requests += <NUM_LIT:1> <EOL> timeout_obj = self . _get_timeout ( timeout ) <EOL> try : <EOL> timeout_obj . start_connect ( ) <EOL> conn . timeout = timeout_obj . connect_timeout <EOL> conn . request ( method , url , ** httplib_request_kw ) <EOL> except SocketTimeout : <EOL> raise ConnectTimeoutError ( <EOL> self , "<STR_LIT>" % <EOL> ( self . host , timeout_obj . connect_timeout ) ) <EOL> read_timeout = timeout_obj . read_timeout <EOL> if hasattr ( conn , '<STR_LIT>' ) : <EOL> if read_timeout == <NUM_LIT:0> : <EOL> raise ReadTimeoutError ( <EOL> self , url , <EOL> "<STR_LIT>" % read_timeout ) <EOL> if read_timeout is Timeout . DEFAULT_TIMEOUT : <EOL> conn . sock . settimeout ( socket . getdefaulttimeout ( ) ) <EOL> else : <EOL> conn . sock . settimeout ( read_timeout ) <EOL> try : <EOL> try : <EOL> httplib_response = conn . getresponse ( buffering = True ) <EOL> except TypeError : <EOL> httplib_response = conn . getresponse ( ) <EOL> except SocketTimeout : <EOL> raise ReadTimeoutError ( <EOL> self , url , "<STR_LIT>" % read_timeout ) <EOL> except BaseSSLError as e : <EOL> if '<STR_LIT>' in str ( e ) or '<STR_LIT>' in str ( e ) : <EOL> raise ReadTimeoutError ( self , url , "<STR_LIT>" ) <EOL> raise <EOL> except SocketError as e : <EOL> if e . errno in _blocking_errnos : <EOL> raise ReadTimeoutError ( <EOL> self , url , <EOL> "<STR_LIT>" % read_timeout ) <EOL> raise <EOL> http_version = getattr ( conn , '<STR_LIT>' , '<STR_LIT>' ) <EOL> log . debug ( "<STR_LIT>" % ( method , url , http_version , <EOL> httplib_response . status , <EOL> httplib_response . length ) ) <EOL> return httplib_response <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> old_pool , self . pool = self . pool , None <EOL> try : <EOL> while True : <EOL> conn = old_pool . get ( block = False ) <EOL> if conn : <EOL> conn . close ( ) <EOL> except Empty : <EOL> pass <EOL> def is_same_host ( self , url ) : <EOL> """<STR_LIT>""" <EOL> if url . startswith ( '<STR_LIT:/>' ) : <EOL> return True <EOL> scheme , host , port = get_host ( url ) <EOL> if self . port and not port : <EOL> port = port_by_scheme . get ( scheme ) <EOL> return ( scheme , host , port ) == ( self . scheme , self . host , self . port ) <EOL> def urlopen ( self , method , url , body = None , headers = None , retries = <NUM_LIT:3> , <EOL> redirect = True , assert_same_host = True , timeout = _Default , <EOL> pool_timeout = None , release_conn = None , ** response_kw ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> headers = self . headers <EOL> if retries < <NUM_LIT:0> : <EOL> raise MaxRetryError ( self , url ) <EOL> if release_conn is None : <EOL> release_conn = response_kw . get ( '<STR_LIT>' , True ) <EOL> if assert_same_host and not self . is_same_host ( url ) : <EOL> raise HostChangedError ( self , url , retries - <NUM_LIT:1> ) <EOL> conn = None <EOL> if self . scheme == '<STR_LIT:http>' : <EOL> headers = headers . copy ( ) <EOL> headers . update ( self . proxy_headers ) <EOL> try : <EOL> conn = self . _get_conn ( timeout = pool_timeout ) <EOL> httplib_response = self . _make_request ( conn , method , url , <EOL> timeout = timeout , <EOL> body = body , headers = headers ) <EOL> response_conn = not release_conn and conn <EOL> response = HTTPResponse . from_httplib ( httplib_response , <EOL> pool = self , <EOL> connection = response_conn , <EOL> ** response_kw ) <EOL> except Empty : <EOL> raise EmptyPoolError ( self , "<STR_LIT>" ) <EOL> except BaseSSLError as e : <EOL> raise SSLError ( e ) <EOL> except CertificateError as e : <EOL> raise SSLError ( e ) <EOL> except TimeoutError as e : <EOL> conn = None <EOL> err = e <EOL> if retries == <NUM_LIT:0> : <EOL> raise <EOL> except ( HTTPException , SocketError ) as e : <EOL> if isinstance ( e , SocketError ) and self . proxy is not None : <EOL> raise ProxyError ( '<STR_LIT>' <EOL> '<STR_LIT>' % e ) <EOL> conn = None <EOL> err = e <EOL> if retries == <NUM_LIT:0> : <EOL> raise MaxRetryError ( self , url , e ) <EOL> finally : <EOL> if release_conn : <EOL> self . _put_conn ( conn ) <EOL> if not conn : <EOL> log . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( retries , err , url ) ) <EOL> return self . urlopen ( method , url , body , headers , retries - <NUM_LIT:1> , <EOL> redirect , assert_same_host , <EOL> timeout = timeout , pool_timeout = pool_timeout , <EOL> release_conn = release_conn , ** response_kw ) <EOL> redirect_location = redirect and response . get_redirect_location ( ) <EOL> if redirect_location : <EOL> if response . status == <NUM_LIT> : <EOL> method = '<STR_LIT:GET>' <EOL> log . info ( "<STR_LIT>" % ( url , redirect_location ) ) <EOL> return self . urlopen ( method , redirect_location , body , headers , <EOL> retries - <NUM_LIT:1> , redirect , assert_same_host , <EOL> timeout = timeout , pool_timeout = pool_timeout , <EOL> release_conn = release_conn , ** response_kw ) <EOL> return response <EOL> class HTTPSConnectionPool ( HTTPConnectionPool ) : <EOL> """<STR_LIT>""" <EOL> scheme = '<STR_LIT>' <EOL> ConnectionCls = HTTPSConnection <EOL> def __init__ ( self , host , port = None , <EOL> strict = False , timeout = None , maxsize = <NUM_LIT:1> , <EOL> block = False , headers = None , <EOL> _proxy = None , _proxy_headers = None , <EOL> key_file = None , cert_file = None , cert_reqs = None , <EOL> ca_certs = None , ssl_version = None , <EOL> assert_hostname = None , assert_fingerprint = None ) : <EOL> HTTPConnectionPool . __init__ ( self , host , port , strict , timeout , maxsize , <EOL> block , headers , _proxy , _proxy_headers ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . cert_reqs = cert_reqs <EOL> self . ca_certs = ca_certs <EOL> self . ssl_version = ssl_version <EOL> self . assert_hostname = assert_hostname <EOL> self . assert_fingerprint = assert_fingerprint <EOL> def _prepare_conn ( self , conn ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( conn , VerifiedHTTPSConnection ) : <EOL> conn . set_cert ( key_file = self . key_file , <EOL> cert_file = self . cert_file , <EOL> cert_reqs = self . cert_reqs , <EOL> ca_certs = self . ca_certs , <EOL> assert_hostname = self . assert_hostname , <EOL> assert_fingerprint = self . assert_fingerprint ) <EOL> conn . ssl_version = self . ssl_version <EOL> if self . proxy is not None : <EOL> try : <EOL> set_tunnel = conn . set_tunnel <EOL> except AttributeError : <EOL> set_tunnel = conn . _set_tunnel <EOL> set_tunnel ( self . host , self . port , self . proxy_headers ) <EOL> conn . connect ( ) <EOL> return conn <EOL> def _new_conn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . num_connections += <NUM_LIT:1> <EOL> log . info ( "<STR_LIT>" <EOL> % ( self . num_connections , self . host ) ) <EOL> if not self . ConnectionCls or self . ConnectionCls is DummyConnection : <EOL> raise SSLError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> actual_host = self . host <EOL> actual_port = self . port <EOL> if self . proxy is not None : <EOL> actual_host = self . proxy . host <EOL> actual_port = self . proxy . port <EOL> extra_params = { } <EOL> if not six . PY3 : <EOL> extra_params [ '<STR_LIT:strict>' ] = self . strict <EOL> conn = self . ConnectionCls ( host = actual_host , port = actual_port , <EOL> timeout = self . timeout . connect_timeout , <EOL> ** extra_params ) <EOL> return self . _prepare_conn ( conn ) <EOL> def connection_from_url ( url , ** kw ) : <EOL> """<STR_LIT>""" <EOL> scheme , host , port = get_host ( url ) <EOL> if scheme == '<STR_LIT>' : <EOL> return HTTPSConnectionPool ( host , port = port , ** kw ) <EOL> else : <EOL> return HTTPConnectionPool ( host , port = port , ** kw ) </s>
