#NEWLINE# def _encode_multipart( vars , content_type , fout = None ) : #NEWLINE# #INDENT# 'Encode a multipart request body into a string' #NEWLINE# f = ( fout or io . BytesIO ( ) ) #NEWLINE# w = f . write #NEWLINE# wt = ( lambda t : f . write ( t . encode ( 'utf8' ) ) ) #NEWLINE# CRLF = b'\r\n' #NEWLINE# boundary = _get_multipart_boundary ( content_type ) #NEWLINE# if ( not boundary ) : #NEWLINE# #INDENT# boundary = native_ ( binascii . hexlify ( os . urandom ( 10 ) ) ) #NEWLINE# content_type += ( '; boundary=%s' % boundary ) #NEWLINE# #UNINDENT# for ( name , value ) in vars : #NEWLINE# #INDENT# w ( b'--' ) #NEWLINE# wt ( boundary ) #NEWLINE# w ( CRLF ) #NEWLINE# assert ( name is not None ) , ( 'Value associated with no name: %r' % value ) #NEWLINE# wt ( ( 'Content-Disposition: form-data; name="%s"' % name ) ) #NEWLINE# filename = None #NEWLINE# if getattr ( value , 'filename' , None ) : #NEWLINE# #INDENT# filename = value . filename #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if isinstance ( value , ( list , tuple ) ) : #NEWLINE# #INDENT# ( filename , value ) = value #NEWLINE# if hasattr ( value , 'read' ) : #NEWLINE# #INDENT# value = value . read ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( filename is not None ) : #NEWLINE# #INDENT# wt ( ( '; filename="%s"' % filename ) ) #NEWLINE# mime_type = mimetypes . guess_type ( filename ) [ 0 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# mime_type = None #NEWLINE# #UNINDENT# w ( CRLF ) #NEWLINE# if getattr ( value , 'type' , None ) : #NEWLINE# #INDENT# wt ( ( 'Content-type: %s' % value . type ) ) #NEWLINE# if value . type_options : #NEWLINE# #INDENT# for ( ct_name , ct_value ) in sorted ( value . type_options . items ( ) ) : #NEWLINE# #INDENT# wt ( ( '; %s="%s"' % ( ct_name , ct_value ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# w ( CRLF ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if mime_type : #NEWLINE# #INDENT# wt ( ( 'Content-type: %s' % mime_type ) ) #NEWLINE# w ( CRLF ) #NEWLINE# #UNINDENT# #UNINDENT# w ( CRLF ) #NEWLINE# if hasattr ( value , 'value' ) : #NEWLINE# #INDENT# value = name . value #NEWLINE# #UNINDENT# if isinstance ( value , bytes ) : #NEWLINE# #INDENT# w ( value ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# wt ( value ) #NEWLINE# #UNINDENT# w ( CRLF ) #NEWLINE# #UNINDENT# wt ( ( '--%s--' % boundary ) ) #NEWLINE# if fout : #NEWLINE# #INDENT# return ( content_type , fout ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return ( content_type , f . getvalue ( ) )