<s> from __future__ import division <EOL> import httplib <EOL> import urllib2 <EOL> import ssl <EOL> import certifi <EOL> from backports . ssl_match_hostname import match_hostname <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class CertValidatingHTTPSConnection ( httplib . HTTPConnection ) : <EOL> default_port = httplib . HTTPS_PORT <EOL> def __init__ ( self , host , port = None , key_file = None , cert_file = None , <EOL> ca_certs = None , strict = None , ** kwargs ) : <EOL> httplib . HTTPConnection . __init__ ( self , host , port , strict , ** kwargs ) <EOL> self . key_file = key_file <EOL> self . cert_file = cert_file <EOL> self . ca_certs = ca_certs <EOL> if self . ca_certs : <EOL> self . cert_reqs = ssl . CERT_REQUIRED <EOL> else : <EOL> self . cert_reqs = ssl . CERT_NONE <EOL> def connect ( self ) : <EOL> httplib . HTTPConnection . connect ( self ) <EOL> self . sock = ssl . wrap_socket ( self . sock , keyfile = self . key_file , <EOL> certfile = self . cert_file , <EOL> cert_reqs = self . cert_reqs , <EOL> ca_certs = self . ca_certs ) <EOL> if self . cert_reqs & ssl . CERT_REQUIRED : <EOL> cert = self . sock . getpeercert ( ) <EOL> hostname = self . host . split ( '<STR_LIT::>' , <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> match_hostname ( cert , hostname ) <EOL> class VerifiedHTTPSHandler ( urllib2 . HTTPSHandler ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> urllib2 . HTTPSHandler . __init__ ( self ) <EOL> self . _connection_args = kwargs <EOL> def https_open ( self , req ) : <EOL> def http_class_wrapper ( host , ** kwargs ) : <EOL> full_kwargs = dict ( self . _connection_args ) <EOL> full_kwargs . update ( kwargs ) <EOL> return CertValidatingHTTPSConnection ( host , ** full_kwargs ) <EOL> return self . do_open ( http_class_wrapper , req ) <EOL> https_handler = VerifiedHTTPSHandler ( ca_certs = certifi . where ( ) ) <EOL> def build_opener ( * handlers , ** kwargs ) : <EOL> insecure = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if kwargs : <EOL> raise TypeError ( "<STR_LIT>" % <EOL> next ( iter ( kwargs ) ) ) <EOL> if not insecure : <EOL> handlers = handlers + ( https_handler , ) <EOL> handlers = handlers + ( urllib2 . ProxyHandler ( ) , ) <EOL> return urllib2 . build_opener ( * handlers ) </s>
