<s> """<STR_LIT>""" <EOL> __author__ = "<STR_LIT>" <EOL> __date__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __license__ = "<STR_LIT>" <EOL> __contact__ = "<STR_LIT>" <EOL> __revision__ = '<STR_LIT>' <EOL> import logging <EOL> import socket <EOL> import sys <EOL> if sys . version_info [ <NUM_LIT:0> ] > <NUM_LIT:2> : <EOL> from http . client import HTTPS_PORT <EOL> from http . client import HTTPConnection <EOL> from urllib . request import AbstractHTTPHandler <EOL> else : <EOL> from httplib import HTTPS_PORT <EOL> from httplib import HTTPConnection <EOL> from urllib2 import AbstractHTTPHandler <EOL> from OpenSSL import SSL <EOL> from ndg . httpsclient . ssl_socket import SSLSocket <EOL> log = logging . getLogger ( __name__ ) <EOL> class HTTPSConnection ( HTTPConnection ) : <EOL> """<STR_LIT>""" <EOL> default_port = HTTPS_PORT <EOL> default_ssl_method = SSL . SSLv23_METHOD <EOL> def __init__ ( self , host , port = None , strict = None , <EOL> timeout = socket . _GLOBAL_DEFAULT_TIMEOUT , ssl_context = None ) : <EOL> HTTPConnection . __init__ ( self , host , port , strict , timeout ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . ssl_context = None <EOL> if ssl_context is not None : <EOL> if not isinstance ( ssl_context , SSL . Context ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ssl_context ) <EOL> self . ssl_context = ssl_context <EOL> def connect ( self ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> if not isinstance ( self . ssl_context , SSL . Context ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> self . ssl_context ) <EOL> ssl_context = self . ssl_context <EOL> else : <EOL> ssl_context = SSL . Context ( self . __class__ . default_ssl_method ) <EOL> sock = socket . create_connection ( ( self . host , self . port ) , self . timeout ) <EOL> if getattr ( self , '<STR_LIT>' , None ) : <EOL> self . sock = sock <EOL> self . _tunnel ( ) <EOL> self . sock = SSLSocket ( ssl_context , sock ) <EOL> self . sock . set_connect_state ( ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self . sock , "<STR_LIT>" ) : <EOL> self . sock . close ( ) <EOL> class HTTPSContextHandler ( AbstractHTTPHandler ) : <EOL> '''<STR_LIT>''' <EOL> https_request = AbstractHTTPHandler . do_request_ <EOL> def __init__ ( self , ssl_context , debuglevel = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> AbstractHTTPHandler . __init__ ( self , debuglevel ) <EOL> if ssl_context is not None : <EOL> if not isinstance ( ssl_context , SSL . Context ) : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ssl_context ) <EOL> self . ssl_context = ssl_context <EOL> else : <EOL> self . ssl_context = SSL . Context ( SSL . TLSv1_METHOD ) <EOL> def https_open ( self , req ) : <EOL> """<STR_LIT>""" <EOL> customHTTPSContextConnection = type ( '<STR_LIT>' , <EOL> ( HTTPSConnection , object ) , <EOL> { '<STR_LIT>' : self . ssl_context } ) <EOL> return self . do_open ( customHTTPSContextConnection , req ) </s>
