<s> import logging <EOL> try : <EOL> from urllib . parse import urljoin <EOL> except ImportError : <EOL> from urlparse import urljoin <EOL> from . _collections import RecentlyUsedContainer <EOL> from . connectionpool import HTTPConnectionPool , HTTPSConnectionPool <EOL> from . connectionpool import port_by_scheme <EOL> from . exceptions import LocationValueError , MaxRetryError , ProxySchemeUnknown <EOL> from . request import RequestMethods <EOL> from . util . url import parse_url <EOL> from . util . retry import Retry <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> pool_classes_by_scheme = { <EOL> '<STR_LIT:http>' : HTTPConnectionPool , <EOL> '<STR_LIT>' : HTTPSConnectionPool , <EOL> } <EOL> log = logging . getLogger ( __name__ ) <EOL> SSL_KEYWORDS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> class PoolManager ( RequestMethods ) : <EOL> """<STR_LIT>""" <EOL> proxy = None <EOL> def __init__ ( self , num_pools = <NUM_LIT:10> , headers = None , ** connection_pool_kw ) : <EOL> RequestMethods . __init__ ( self , headers ) <EOL> self . connection_pool_kw = connection_pool_kw <EOL> self . pools = RecentlyUsedContainer ( num_pools , <EOL> dispose_func = lambda p : p . close ( ) ) <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . clear ( ) <EOL> return False <EOL> def _new_pool ( self , scheme , host , port ) : <EOL> """<STR_LIT>""" <EOL> pool_cls = pool_classes_by_scheme [ scheme ] <EOL> kwargs = self . connection_pool_kw <EOL> if scheme == '<STR_LIT:http>' : <EOL> kwargs = self . connection_pool_kw . copy ( ) <EOL> for kw in SSL_KEYWORDS : <EOL> kwargs . pop ( kw , None ) <EOL> return pool_cls ( host , port , ** kwargs ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . pools . clear ( ) <EOL> def connection_from_host ( self , host , port = None , scheme = '<STR_LIT:http>' ) : <EOL> """<STR_LIT>""" <EOL> if not host : <EOL> raise LocationValueError ( "<STR_LIT>" ) <EOL> scheme = scheme or '<STR_LIT:http>' <EOL> port = port or port_by_scheme . get ( scheme , <NUM_LIT> ) <EOL> pool_key = ( scheme , host , port ) <EOL> with self . pools . lock : <EOL> pool = self . pools . get ( pool_key ) <EOL> if pool : <EOL> return pool <EOL> pool = self . _new_pool ( scheme , host , port ) <EOL> self . pools [ pool_key ] = pool <EOL> return pool <EOL> def connection_from_url ( self , url ) : <EOL> """<STR_LIT>""" <EOL> u = parse_url ( url ) <EOL> return self . connection_from_host ( u . host , port = u . port , scheme = u . scheme ) <EOL> def urlopen ( self , method , url , redirect = True , ** kw ) : <EOL> """<STR_LIT>""" <EOL> u = parse_url ( url ) <EOL> conn = self . connection_from_host ( u . host , port = u . port , scheme = u . scheme ) <EOL> kw [ '<STR_LIT>' ] = False <EOL> kw [ '<STR_LIT>' ] = False <EOL> if '<STR_LIT>' not in kw : <EOL> kw [ '<STR_LIT>' ] = self . headers <EOL> if self . proxy is not None and u . scheme == "<STR_LIT:http>" : <EOL> response = conn . urlopen ( method , url , ** kw ) <EOL> else : <EOL> response = conn . urlopen ( method , u . request_uri , ** kw ) <EOL> redirect_location = redirect and response . get_redirect_location ( ) <EOL> if not redirect_location : <EOL> return response <EOL> redirect_location = urljoin ( url , redirect_location ) <EOL> if response . status == <NUM_LIT> : <EOL> method = '<STR_LIT:GET>' <EOL> retries = kw . get ( '<STR_LIT>' ) <EOL> if not isinstance ( retries , Retry ) : <EOL> retries = Retry . from_int ( retries , redirect = redirect ) <EOL> try : <EOL> retries = retries . increment ( method , url , response = response , _pool = conn ) <EOL> except MaxRetryError : <EOL> if retries . raise_on_redirect : <EOL> raise <EOL> return response <EOL> kw [ '<STR_LIT>' ] = retries <EOL> kw [ '<STR_LIT>' ] = redirect <EOL> log . info ( "<STR_LIT>" % ( url , redirect_location ) ) <EOL> return self . urlopen ( method , redirect_location , ** kw ) <EOL> class ProxyManager ( PoolManager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , proxy_url , num_pools = <NUM_LIT:10> , headers = None , <EOL> proxy_headers = None , ** connection_pool_kw ) : <EOL> if isinstance ( proxy_url , HTTPConnectionPool ) : <EOL> proxy_url = '<STR_LIT>' % ( proxy_url . scheme , proxy_url . host , <EOL> proxy_url . port ) <EOL> proxy = parse_url ( proxy_url ) <EOL> if not proxy . port : <EOL> port = port_by_scheme . get ( proxy . scheme , <NUM_LIT> ) <EOL> proxy = proxy . _replace ( port = port ) <EOL> if proxy . scheme not in ( "<STR_LIT:http>" , "<STR_LIT>" ) : <EOL> raise ProxySchemeUnknown ( proxy . scheme ) <EOL> self . proxy = proxy <EOL> self . proxy_headers = proxy_headers or { } <EOL> connection_pool_kw [ '<STR_LIT>' ] = self . proxy <EOL> connection_pool_kw [ '<STR_LIT>' ] = self . proxy_headers <EOL> super ( ProxyManager , self ) . __init__ ( <EOL> num_pools , headers , ** connection_pool_kw ) <EOL> def connection_from_host ( self , host , port = None , scheme = '<STR_LIT:http>' ) : <EOL> if scheme == "<STR_LIT>" : <EOL> return super ( ProxyManager , self ) . connection_from_host ( <EOL> host , port , scheme ) <EOL> return super ( ProxyManager , self ) . connection_from_host ( <EOL> self . proxy . host , self . proxy . port , self . proxy . scheme ) <EOL> def _set_proxy_headers ( self , url , headers = None ) : <EOL> """<STR_LIT>""" <EOL> headers_ = { '<STR_LIT>' : '<STR_LIT>' } <EOL> netloc = parse_url ( url ) . netloc <EOL> if netloc : <EOL> headers_ [ '<STR_LIT>' ] = netloc <EOL> if headers : <EOL> headers_ . update ( headers ) <EOL> return headers_ <EOL> def urlopen ( self , method , url , redirect = True , ** kw ) : <EOL> "<STR_LIT>" <EOL> u = parse_url ( url ) <EOL> if u . scheme == "<STR_LIT:http>" : <EOL> headers = kw . get ( '<STR_LIT>' , self . headers ) <EOL> kw [ '<STR_LIT>' ] = self . _set_proxy_headers ( url , headers ) <EOL> return super ( ProxyManager , self ) . urlopen ( method , url , redirect = redirect , ** kw ) <EOL> def proxy_from_url ( url , ** kw ) : <EOL> return ProxyManager ( proxy_url = url , ** kw ) </s>
