<s> import httplib <EOL> import os <EOL> import socket <EOL> import sys <EOL> import urllib <EOL> import urlparse <EOL> import warnings <EOL> from cStringIO import StringIO <EOL> from TTransport import * <EOL> class THttpClient ( TTransportBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , uri_or_host , port = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> if port is not None : <EOL> warnings . warn ( <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> self . host = uri_or_host <EOL> self . port = port <EOL> assert path <EOL> self . path = path <EOL> self . scheme = '<STR_LIT:http>' <EOL> else : <EOL> parsed = urlparse . urlparse ( uri_or_host ) <EOL> self . scheme = parsed . scheme <EOL> assert self . scheme in ( '<STR_LIT:http>' , '<STR_LIT>' ) <EOL> if self . scheme == '<STR_LIT:http>' : <EOL> self . port = parsed . port or httplib . HTTP_PORT <EOL> elif self . scheme == '<STR_LIT>' : <EOL> self . port = parsed . port or httplib . HTTPS_PORT <EOL> self . host = parsed . hostname <EOL> self . path = parsed . path <EOL> if parsed . query : <EOL> self . path += '<STR_LIT>' % parsed . query <EOL> self . __wbuf = StringIO ( ) <EOL> self . __http = None <EOL> self . __timeout = None <EOL> self . __custom_headers = None <EOL> def open ( self ) : <EOL> if self . scheme == '<STR_LIT:http>' : <EOL> self . __http = httplib . HTTP ( self . host , self . port ) <EOL> else : <EOL> self . __http = httplib . HTTPS ( self . host , self . port ) <EOL> def close ( self ) : <EOL> self . __http . close ( ) <EOL> self . __http = None <EOL> def isOpen ( self ) : <EOL> return self . __http is not None <EOL> def setTimeout ( self , ms ) : <EOL> if not hasattr ( socket , '<STR_LIT>' ) : <EOL> raise NotImplementedError <EOL> if ms is None : <EOL> self . __timeout = None <EOL> else : <EOL> self . __timeout = ms / <NUM_LIT> <EOL> def setCustomHeaders ( self , headers ) : <EOL> self . __custom_headers = headers <EOL> def read ( self , sz ) : <EOL> return self . __http . file . read ( sz ) <EOL> def write ( self , buf ) : <EOL> self . __wbuf . write ( buf ) <EOL> def __withTimeout ( f ) : <EOL> def _f ( * args , ** kwargs ) : <EOL> orig_timeout = socket . getdefaulttimeout ( ) <EOL> socket . setdefaulttimeout ( args [ <NUM_LIT:0> ] . __timeout ) <EOL> result = f ( * args , ** kwargs ) <EOL> socket . setdefaulttimeout ( orig_timeout ) <EOL> return result <EOL> return _f <EOL> def flush ( self ) : <EOL> if self . isOpen ( ) : <EOL> self . close ( ) <EOL> self . open ( ) <EOL> data = self . __wbuf . getvalue ( ) <EOL> self . __wbuf = StringIO ( ) <EOL> self . __http . putrequest ( '<STR_LIT:POST>' , self . path ) <EOL> self . __http . putheader ( '<STR_LIT>' , self . host ) <EOL> self . __http . putheader ( '<STR_LIT:Content-Type>' , '<STR_LIT>' ) <EOL> self . __http . putheader ( '<STR_LIT>' , str ( len ( data ) ) ) <EOL> if not self . __custom_headers or '<STR_LIT>' not in self . __custom_headers : <EOL> user_agent = '<STR_LIT>' <EOL> script = os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) <EOL> if script : <EOL> user_agent = '<STR_LIT>' % ( user_agent , urllib . quote ( script ) ) <EOL> self . __http . putheader ( '<STR_LIT>' , user_agent ) <EOL> if self . __custom_headers : <EOL> for key , val in self . __custom_headers . iteritems ( ) : <EOL> self . __http . putheader ( key , val ) <EOL> self . __http . endheaders ( ) <EOL> self . __http . send ( data ) <EOL> self . code , self . message , self . headers = self . __http . getreply ( ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> flush = __withTimeout ( flush ) </s>
