<s> '''<STR_LIT>''' <EOL> import mimetools <EOL> import mimetypes <EOL> import sys <EOL> import urllib <EOL> import urllib2 <EOL> class Callable : <EOL> def __init__ ( self , anycallable ) : <EOL> self . __call__ = anycallable <EOL> class MultipartPostHandler ( urllib2 . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> handler_order = urllib2 . HTTPHandler . handler_order - <NUM_LIT:10> <EOL> doseq = <NUM_LIT:1> <EOL> def http_request ( self , request ) : <EOL> data = request . get_data ( ) <EOL> if data is not None and type ( data ) != str : <EOL> v_files = [ ] <EOL> v_vars = [ ] <EOL> try : <EOL> for ( key , value ) in data . items ( ) : <EOL> if type ( value ) == file : <EOL> v_files . append ( ( key , value ) ) <EOL> else : <EOL> v_vars . append ( ( key , value ) ) <EOL> except TypeError : <EOL> systype , value , traceback = sys . exc_info ( ) <EOL> raise TypeError ( "<STR_LIT>" ) , traceback <EOL> if len ( v_files ) == <NUM_LIT:0> : <EOL> data = urllib . urlencode ( v_vars , self . doseq ) <EOL> else : <EOL> boundary , data = self . multipart_encode ( v_vars , v_files ) <EOL> contenttype = '<STR_LIT>' % boundary <EOL> if ( request . has_header ( '<STR_LIT:Content-Type>' ) <EOL> and request . get_header ( '<STR_LIT:Content-Type>' ) . find ( '<STR_LIT>' ) != <NUM_LIT:0> ) : <EOL> print "<STR_LIT>" % ( request . get_header ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> request . add_unredirected_header ( '<STR_LIT:Content-Type>' , contenttype ) <EOL> request . add_data ( data ) <EOL> return request <EOL> def multipart_encode ( vars , files , boundary = None , buffer = None ) : <EOL> if boundary is None : <EOL> boundary = mimetools . choose_boundary ( ) <EOL> if buffer is None : <EOL> buffer = '<STR_LIT>' <EOL> for ( key , value ) in vars : <EOL> buffer += '<STR_LIT>' % boundary <EOL> buffer += '<STR_LIT>' % key <EOL> buffer += '<STR_LIT>' + value + '<STR_LIT:\r\n>' <EOL> for ( key , fd ) in files : <EOL> filename = fd . name . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> contenttype = mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> buffer += '<STR_LIT>' % boundary <EOL> buffer += '<STR_LIT>' % ( key , filename ) <EOL> buffer += '<STR_LIT>' % contenttype <EOL> fd . seek ( <NUM_LIT:0> ) <EOL> buffer += '<STR_LIT:\r\n>' + fd . read ( ) + '<STR_LIT:\r\n>' <EOL> buffer += '<STR_LIT>' % boundary <EOL> return boundary , buffer <EOL> multipart_encode = Callable ( multipart_encode ) <EOL> https_request = http_request </s>
