<s> """<STR_LIT>""" <EOL> import httplib <EOL> import diesel <EOL> from diesel . resolver import DNSResolutionError <EOL> from OpenSSL import SSL <EOL> try : <EOL> from requests . packages . urllib3 import connectionpool <EOL> import requests <EOL> except ImportError : <EOL> connectionpool = None <EOL> class SocketLike ( diesel . Client ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , ** kw ) : <EOL> super ( SocketLike , self ) . __init__ ( host , port , ** kw ) <EOL> self . _timeout = None <EOL> def makefile ( self , mode , buffering ) : <EOL> return FileLike ( self , mode , buffering , self . _timeout ) <EOL> def settimeout ( self , n ) : <EOL> self . _timeout = n <EOL> @ diesel . call <EOL> def sendall ( self , data ) : <EOL> diesel . send ( data ) <EOL> def fileno ( self ) : <EOL> return id ( self ) <EOL> class FileLike ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , client , mode , buffering , timeout ) : <EOL> self . _client = client <EOL> self . mode = mode <EOL> self . buffering = buffering <EOL> self . _extra = "<STR_LIT>" <EOL> self . _timeout = timeout <EOL> @ property <EOL> def conn ( self ) : <EOL> return self . _client . conn <EOL> @ property <EOL> def connected ( self ) : <EOL> return self . _client . connected <EOL> @ property <EOL> def is_closed ( self ) : <EOL> return self . _client . is_closed <EOL> @ diesel . call <EOL> def read ( self , size = None ) : <EOL> assert size is not None , "<STR_LIT>" <EOL> if size == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> evt , data = diesel . first ( sleep = self . _timeout , receive = size ) <EOL> if evt == '<STR_LIT>' : <EOL> self . _timeout = None <EOL> raise requests . exceptions . Timeout <EOL> return data <EOL> @ diesel . call <EOL> def readline ( self , max_size = None ) : <EOL> evt , line = diesel . first ( sleep = self . _timeout , until = '<STR_LIT:\n>' ) <EOL> if evt == '<STR_LIT>' : <EOL> self . _timeout = None <EOL> raise requests . exceptions . Timeout <EOL> if max_size : <EOL> line = "<STR_LIT>" . join ( [ self . _extra , line ] ) <EOL> nl = line . find ( '<STR_LIT:\n>' ) + <NUM_LIT:1> <EOL> if nl > max_size : <EOL> nl = max_size <EOL> line , self . _extra = line [ : nl ] , line [ nl : ] <EOL> return line <EOL> @ diesel . call <EOL> def write ( self , data ) : <EOL> diesel . send ( data ) <EOL> @ diesel . call <EOL> def next ( self ) : <EOL> data = self . readline ( ) <EOL> if not data : <EOL> raise StopIteration ( ) <EOL> return data <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def close ( self ) : <EOL> self . _client . close ( ) <EOL> class HTTPConnection ( httplib . HTTPConnection ) : <EOL> def connect ( self ) : <EOL> try : <EOL> self . sock = SocketLike ( self . host , self . port ) <EOL> except DNSResolutionError : <EOL> raise requests . ConnectionError <EOL> class HTTPSConnection ( httplib . HTTPSConnection ) : <EOL> def connect ( self ) : <EOL> try : <EOL> kw = { '<STR_LIT>' : SSL . Context ( SSL . SSLv23_METHOD ) } <EOL> self . sock = SocketLike ( self . host , self . port , ** kw ) <EOL> except DNSResolutionError : <EOL> raise requests . ConnectionError <EOL> class RequestsLibNotFound ( Exception ) : pass <EOL> def enable_requests ( ) : <EOL> """<STR_LIT>""" <EOL> if not connectionpool : <EOL> msg = "<STR_LIT>" <EOL> raise RequestsLibNotFound ( msg ) <EOL> connectionpool . HTTPConnection = HTTPConnection <EOL> connectionpool . HTTPSConnection = HTTPSConnection </s>
