<s> import logging <EOL> import socket <EOL> import errno <EOL> from socket import error as SocketError , timeout as SocketTimeout <EOL> try : <EOL> from http . client import HTTPConnection , HTTPException <EOL> from http . client import HTTP_PORT , HTTPS_PORT <EOL> except ImportError : <EOL> from httplib import HTTPConnection , HTTPException <EOL> from httplib import HTTP_PORT , HTTPS_PORT <EOL> try : <EOL> from queue import LifoQueue , Empty , Full <EOL> except ImportError : <EOL> from Queue import LifoQueue , Empty , Full <EOL> try : <EOL> HTTPSConnection = object <EOL> BaseSSLError = None <EOL> ssl = None <EOL> try : <EOL> from http . client import HTTPSConnection <EOL> except ImportError : <EOL> from httplib import HTTPSConnection <EOL> import ssl <EOL> BaseSSLError = ssl . SSLError <EOL> except ( ImportError , AttributeError ) : <EOL> pass <EOL> from . request import RequestMethods <EOL> from . response import HTTPResponse <EOL> from . util import get_host , is_connection_dropped , ssl_wrap_socket <EOL> from . exceptions import ( <EOL> ClosedPoolError , <EOL> EmptyPoolError , <EOL> HostChangedError , <EOL> MaxRetryError , <EOL> SSLError , <EOL> TimeoutError , <EOL> ) <EOL> from . packages . ssl_match_hostname import match_hostname , CertificateError <EOL> from . packages import six <EOL> xrange = six . moves . xrange <EOL> log = logging . getLogger ( __name__ ) <EOL> _Default = object ( ) <EOL> port_by_scheme = { <EOL> '<STR_LIT:http>' : HTTP_PORT , <EOL> '<STR_LIT>' : HTTPS_PORT , <EOL> } <EOL> class VerifiedHTTPSConnection ( HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> cert_reqs = None <EOL> ca_certs = None <EOL> ssl_version = None <EOL> def set_cert ( self , key_file = None , cert_file = None , <EOL> cert_reqs = '<STR_LIT>' , ca_certs = None ) : <EOL> ssl_req_scheme = { <EOL> '<STR_LIT>' : ssl . CERT_NONE , <EOL> '<STR_LIT>' : ssl . CERT_OPTIONAL , <EOL> '<STR_LIT>' : ssl . CERT_REQUIRED <EOL> } <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . cert_reqs = ssl_req_scheme . get ( cert_reqs ) or ssl . CERT_NONE <EOL> self . ca_certs = ca_certs <EOL> def connect ( self ) : <EOL> sock = socket . create_connection ( ( self . host , self . port ) , self . timeout ) <EOL> self . sock = ssl_wrap_socket ( sock , self . key_file , self . cert_file , <EOL> cert_reqs = self . cert_reqs , <EOL> ca_certs = self . ca_certs , <EOL> server_hostname = self . host , <EOL> ssl_version = self . ssl_version ) <EOL> if self . ca_certs : <EOL> match_hostname ( self . sock . getpeercert ( ) , self . host ) <EOL> class ConnectionPool ( object ) : <EOL> """<STR_LIT>""" <EOL> scheme = None <EOL> QueueCls = LifoQueue <EOL> def __init__ ( self , host , port = None ) : <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> class HTTPConnectionPool ( ConnectionPool , RequestMethods ) : <EOL> """<STR_LIT>""" <EOL> scheme = '<STR_LIT:http>' <EOL> def __init__ ( self , host , port = None , strict = False , timeout = None , maxsize = <NUM_LIT:1> , <EOL> block = False , headers = None ) : <EOL> ConnectionPool . __init__ ( self , host , port ) <EOL> RequestMethods . __init__ ( self , headers ) <EOL> self . strict = strict <EOL> self . timeout = timeout <EOL> self . pool = self . QueueCls ( maxsize ) <EOL> self . block = block <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> return HTTPConnection ( host = self . host , <EOL> port = self . port , <EOL> strict = self . strict ) <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> conn . close ( ) <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> if timeout is _Default : <EOL> timeout = self . timeout <EOL> conn . timeout = timeout <EOL> conn . request ( method , url , ** httplib_request_kw ) <EOL> sock = getattr ( conn , '<STR_LIT>' , False ) <EOL> if sock : <EOL> sock . settimeout ( timeout ) <EOL> try : <EOL> httplib_response = conn . getresponse ( buffering = True ) <EOL> except TypeError : <EOL> httplib_response = conn . getresponse ( ) <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 timeout is _Default : <EOL> timeout = self . timeout <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> host = "<STR_LIT>" % ( self . scheme , self . host ) <EOL> if self . port : <EOL> host = "<STR_LIT>" % ( host , self . port ) <EOL> raise HostChangedError ( self , url , retries - <NUM_LIT:1> ) <EOL> conn = None <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 as e : <EOL> raise TimeoutError ( self , "<STR_LIT>" % <EOL> pool_timeout ) <EOL> except SocketTimeout as e : <EOL> raise TimeoutError ( self , "<STR_LIT>" % <EOL> timeout ) <EOL> except BaseSSLError as e : <EOL> raise SSLError ( e ) <EOL> except CertificateError as e : <EOL> raise SSLError ( e ) <EOL> except ( HTTPException , SocketError ) as 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> def __init__ ( self , host , port = None , <EOL> strict = False , timeout = None , maxsize = <NUM_LIT:1> , <EOL> block = False , headers = None , <EOL> key_file = None , cert_file = None , <EOL> cert_reqs = '<STR_LIT>' , ca_certs = None , ssl_version = None ) : <EOL> HTTPConnectionPool . __init__ ( self , host , port , <EOL> strict , timeout , maxsize , <EOL> block , 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> 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 ssl : <EOL> if not HTTPSConnection or HTTPSConnection is object : <EOL> raise SSLError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return HTTPSConnection ( host = self . host , <EOL> port = self . port , <EOL> strict = self . strict ) <EOL> connection = VerifiedHTTPSConnection ( host = self . host , <EOL> port = self . port , <EOL> strict = self . strict ) <EOL> connection . set_cert ( key_file = self . key_file , cert_file = self . cert_file , <EOL> cert_reqs = self . cert_reqs , ca_certs = self . ca_certs ) <EOL> if self . ssl_version is None : <EOL> connection . ssl_version = ssl . PROTOCOL_SSLv23 <EOL> else : <EOL> connection . ssl_version = self . ssl_version <EOL> return connection <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>
