<s> """<STR_LIT>""" <EOL> import socket <EOL> from . models import Response <EOL> from . packages . urllib3 . poolmanager import PoolManager , proxy_from_url <EOL> from . packages . urllib3 . response import HTTPResponse <EOL> from . packages . urllib3 . util import Timeout as TimeoutSauce <EOL> from . compat import urlparse , basestring , urldefrag , unquote <EOL> from . utils import ( DEFAULT_CA_BUNDLE_PATH , get_encoding_from_headers , <EOL> prepend_scheme_if_needed , get_auth_from_url ) <EOL> from . structures import CaseInsensitiveDict <EOL> from . packages . urllib3 . exceptions import MaxRetryError <EOL> from . packages . urllib3 . exceptions import TimeoutError <EOL> from . packages . urllib3 . exceptions import SSLError as _SSLError <EOL> from . packages . urllib3 . exceptions import HTTPError as _HTTPError <EOL> from . packages . urllib3 . exceptions import ProxyError as _ProxyError <EOL> from . cookies import extract_cookies_to_jar <EOL> from . exceptions import ConnectionError , Timeout , SSLError , ProxyError <EOL> from . auth import _basic_auth_str <EOL> DEFAULT_POOLBLOCK = False <EOL> DEFAULT_POOLSIZE = <NUM_LIT:10> <EOL> DEFAULT_RETRIES = <NUM_LIT:0> <EOL> class BaseAdapter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( BaseAdapter , self ) . __init__ ( ) <EOL> def send ( self ) : <EOL> raise NotImplementedError <EOL> def close ( self ) : <EOL> raise NotImplementedError <EOL> class HTTPAdapter ( BaseAdapter ) : <EOL> """<STR_LIT>""" <EOL> __attrs__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> def __init__ ( self , pool_connections = DEFAULT_POOLSIZE , <EOL> pool_maxsize = DEFAULT_POOLSIZE , max_retries = DEFAULT_RETRIES , <EOL> pool_block = DEFAULT_POOLBLOCK ) : <EOL> self . max_retries = max_retries <EOL> self . config = { } <EOL> self . proxy_manager = { } <EOL> super ( HTTPAdapter , self ) . __init__ ( ) <EOL> self . _pool_connections = pool_connections <EOL> self . _pool_maxsize = pool_maxsize <EOL> self . _pool_block = pool_block <EOL> self . init_poolmanager ( pool_connections , pool_maxsize , block = pool_block ) <EOL> def __getstate__ ( self ) : <EOL> return dict ( ( attr , getattr ( self , attr , None ) ) for attr in <EOL> self . __attrs__ ) <EOL> def __setstate__ ( self , state ) : <EOL> self . proxy_manager = { } <EOL> self . config = { } <EOL> for attr , value in state . items ( ) : <EOL> setattr ( self , attr , value ) <EOL> self . init_poolmanager ( self . _pool_connections , self . _pool_maxsize , <EOL> block = self . _pool_block ) <EOL> def init_poolmanager ( self , connections , maxsize , block = DEFAULT_POOLBLOCK ) : <EOL> """<STR_LIT>""" <EOL> self . _pool_connections = connections <EOL> self . _pool_maxsize = maxsize <EOL> self . _pool_block = block <EOL> self . poolmanager = PoolManager ( num_pools = connections , maxsize = maxsize , <EOL> block = block ) <EOL> def cert_verify ( self , conn , url , verify , cert ) : <EOL> """<STR_LIT>""" <EOL> if url . lower ( ) . startswith ( '<STR_LIT>' ) and verify : <EOL> cert_loc = None <EOL> if verify is not True : <EOL> cert_loc = verify <EOL> if not cert_loc : <EOL> cert_loc = DEFAULT_CA_BUNDLE_PATH <EOL> if not cert_loc : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> conn . cert_reqs = '<STR_LIT>' <EOL> conn . ca_certs = cert_loc <EOL> else : <EOL> conn . cert_reqs = '<STR_LIT>' <EOL> conn . ca_certs = None <EOL> if cert : <EOL> if not isinstance ( cert , basestring ) : <EOL> conn . cert_file = cert [ <NUM_LIT:0> ] <EOL> conn . key_file = cert [ <NUM_LIT:1> ] <EOL> else : <EOL> conn . cert_file = cert <EOL> def build_response ( self , req , resp ) : <EOL> """<STR_LIT>""" <EOL> response = Response ( ) <EOL> response . status_code = getattr ( resp , '<STR_LIT:status>' , None ) <EOL> response . headers = CaseInsensitiveDict ( getattr ( resp , '<STR_LIT>' , { } ) ) <EOL> response . encoding = get_encoding_from_headers ( response . headers ) <EOL> response . raw = resp <EOL> response . reason = response . raw . reason <EOL> if isinstance ( req . url , bytes ) : <EOL> response . url = req . url . decode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> response . url = req . url <EOL> extract_cookies_to_jar ( response . cookies , req , resp ) <EOL> response . request = req <EOL> response . connection = self <EOL> return response <EOL> def get_connection ( self , url , proxies = None ) : <EOL> """<STR_LIT>""" <EOL> proxies = proxies or { } <EOL> proxy = proxies . get ( urlparse ( url . lower ( ) ) . scheme ) <EOL> if proxy : <EOL> proxy = prepend_scheme_if_needed ( proxy , '<STR_LIT:http>' ) <EOL> proxy_headers = self . proxy_headers ( proxy ) <EOL> if not proxy in self . proxy_manager : <EOL> self . proxy_manager [ proxy ] = proxy_from_url ( <EOL> proxy , <EOL> proxy_headers = proxy_headers , <EOL> num_pools = self . _pool_connections , <EOL> maxsize = self . _pool_maxsize , <EOL> block = self . _pool_block ) <EOL> conn = self . proxy_manager [ proxy ] . connection_from_url ( url ) <EOL> else : <EOL> parsed = urlparse ( url ) <EOL> url = parsed . geturl ( ) <EOL> conn = self . poolmanager . connection_from_url ( url ) <EOL> return conn <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . poolmanager . clear ( ) <EOL> def request_url ( self , request , proxies ) : <EOL> """<STR_LIT>""" <EOL> proxies = proxies or { } <EOL> scheme = urlparse ( request . url ) . scheme <EOL> proxy = proxies . get ( scheme ) <EOL> if proxy and scheme != '<STR_LIT>' : <EOL> url , _ = urldefrag ( request . url ) <EOL> else : <EOL> url = request . path_url <EOL> return url <EOL> def add_headers ( self , request , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def proxy_headers ( self , proxy ) : <EOL> """<STR_LIT>""" <EOL> headers = { } <EOL> username , password = get_auth_from_url ( proxy ) <EOL> if username and password : <EOL> headers [ '<STR_LIT>' ] = _basic_auth_str ( username , <EOL> password ) <EOL> return headers <EOL> def send ( self , request , stream = False , timeout = None , verify = True , cert = None , proxies = None ) : <EOL> """<STR_LIT>""" <EOL> conn = self . get_connection ( request . url , proxies ) <EOL> self . cert_verify ( conn , request . url , verify , cert ) <EOL> url = self . request_url ( request , proxies ) <EOL> self . add_headers ( request ) <EOL> chunked = not ( request . body is None or '<STR_LIT>' in request . headers ) <EOL> timeout = TimeoutSauce ( connect = timeout , read = timeout ) <EOL> try : <EOL> if not chunked : <EOL> resp = conn . urlopen ( <EOL> method = request . method , <EOL> url = url , <EOL> body = request . body , <EOL> headers = request . headers , <EOL> redirect = False , <EOL> assert_same_host = False , <EOL> preload_content = False , <EOL> decode_content = False , <EOL> retries = self . max_retries , <EOL> timeout = timeout <EOL> ) <EOL> else : <EOL> if hasattr ( conn , '<STR_LIT>' ) : <EOL> conn = conn . proxy_pool <EOL> low_conn = conn . _get_conn ( timeout = timeout ) <EOL> try : <EOL> low_conn . putrequest ( request . method , <EOL> url , <EOL> skip_accept_encoding = True ) <EOL> for header , value in request . headers . items ( ) : <EOL> low_conn . putheader ( header , value ) <EOL> low_conn . endheaders ( ) <EOL> for i in request . body : <EOL> low_conn . send ( hex ( len ( i ) ) [ <NUM_LIT:2> : ] . encode ( '<STR_LIT:utf-8>' ) ) <EOL> low_conn . send ( b'<STR_LIT:\r\n>' ) <EOL> low_conn . send ( i ) <EOL> low_conn . send ( b'<STR_LIT:\r\n>' ) <EOL> low_conn . send ( b'<STR_LIT>' ) <EOL> r = low_conn . getresponse ( ) <EOL> resp = HTTPResponse . from_httplib ( <EOL> r , <EOL> pool = conn , <EOL> connection = low_conn , <EOL> preload_content = False , <EOL> decode_content = False <EOL> ) <EOL> except : <EOL> low_conn . close ( ) <EOL> raise <EOL> else : <EOL> conn . _put_conn ( low_conn ) <EOL> except socket . error as sockerr : <EOL> raise ConnectionError ( sockerr , request = request ) <EOL> except MaxRetryError as e : <EOL> raise ConnectionError ( e , request = request ) <EOL> except _ProxyError as e : <EOL> raise ProxyError ( e ) <EOL> except ( _SSLError , _HTTPError ) as e : <EOL> if isinstance ( e , _SSLError ) : <EOL> raise SSLError ( e , request = request ) <EOL> elif isinstance ( e , TimeoutError ) : <EOL> raise Timeout ( e , request = request ) <EOL> else : <EOL> raise <EOL> return self . build_response ( request , resp ) </s>
