<s> import httplib <EOL> import urllib2 <EOL> import socket <EOL> from pdagent . pdagentutil import find_in_sys_path <EOL> from pdagent . thirdparty . ssl_match_hostname import match_hostname , CertificateError <EOL> if not hasattr ( httplib , '<STR_LIT>' ) : <EOL> raise SystemExit ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> try : <EOL> import ssl <EOL> except ImportError : <EOL> raise SystemExit ( <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" <EOL> ) <EOL> DEFAULT_CA_CERTS_FILE = find_in_sys_path ( "<STR_LIT>" ) <EOL> class VerifyingHTTPSConnection ( httplib . HTTPSConnection ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , ** kwargs ) : <EOL> self . ca_certs = kwargs . pop ( "<STR_LIT>" , None ) <EOL> httplib . HTTPSConnection . __init__ ( self , host , ** kwargs ) <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> args = [ ( self . host , self . port ) , self . timeout ] <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> args . append ( self . source_address ) <EOL> sock = socket . create_connection ( * args ) <EOL> if self . _tunnel_host : <EOL> self . sock = sock <EOL> self . _tunnel ( ) <EOL> self . sock = ssl . wrap_socket ( <EOL> sock , <EOL> keyfile = self . key_file , <EOL> certfile = self . cert_file , <EOL> cert_reqs = ssl . CERT_REQUIRED , <EOL> ca_certs = self . ca_certs <EOL> ) <EOL> try : <EOL> match_hostname ( self . sock . getpeercert ( ) , self . host ) <EOL> except CertificateError : <EOL> self . sock . shutdown ( socket . SHUT_RDWR ) <EOL> self . sock . close ( ) <EOL> raise <EOL> class VerifyingHTTPSHandler ( urllib2 . HTTPSHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . ca_certs = kwargs . pop ( "<STR_LIT>" , None ) <EOL> urllib2 . HTTPSHandler . __init__ ( self , ** kwargs ) <EOL> def https_open ( self , req ) : <EOL> return self . do_open ( self . _proxyHTTPSConnection , req ) <EOL> def _proxyHTTPSConnection ( self , host , ** kwargs ) : <EOL> new_kwargs = { <EOL> "<STR_LIT>" : self . ca_certs <EOL> } <EOL> new_kwargs . update ( kwargs ) <EOL> return VerifyingHTTPSConnection ( host , ** new_kwargs ) <EOL> url_opener_cache = dict ( ) <EOL> def urlopen ( url , ** kwargs ) : <EOL> ca_certs = kwargs . pop ( "<STR_LIT>" , DEFAULT_CA_CERTS_FILE ) <EOL> if ca_certs not in url_opener_cache : <EOL> url_opener_cache [ ca_certs ] = urllib2 . build_opener ( VerifyingHTTPSHandler ( ca_certs = ca_certs ) ) <EOL> return url_opener_cache [ ca_certs ] . open ( url , ** kwargs ) </s>
