def encode_multipart(boundary, data):
 lines = []
 for (key, value) in data.items():
  if isinstance(value, file):
   lines.extend([('--' + boundary), ('Content-Disposition: form-data; name="%s"' % key), '', ('--' + boundary), ('Content-Disposition: form-data; name="%s_file"; filename="%s"' % (key, value.name)), 'Content-Type: application/octet-stream', '', value.read()])
  elif hasattr(value, '__iter__'):
   for item in value:
    lines.extend([('--' + boundary), ('Content-Disposition: form-data; name="%s"' % key), '', str(item)])
  else:
   lines.extend([('--' + boundary), ('Content-Disposition: form-data; name="%s"' % key), '', str(value)])
 lines.extend([(('--' + boundary) + '--'), ''])
 return '\r\n'.join(lines)

def encode_multipart ( boundary , data ) : lines = [ ] for ( key , value ) in data . items ( ) : if isinstance ( value , file ) : lines . extend ( [ ( '--' + boundary ) , ( 'Content-Disposition: form-data; name="%s"' % key ) , '' , ( '--' + boundary ) , ( 'Content-Disposition: form-data; name="%s_file"; filename="%s"' % ( key , value . name ) ) , 'Content-Type: application/octet-stream' , '' , value . read ( ) ] ) elif hasattr ( value , '__iter__' ) : for item in value : lines . extend ( [ ( '--' + boundary ) , ( 'Content-Disposition: form-data; name="%s"' % key ) , '' , str ( item ) ] ) else : lines . extend ( [ ( '--' + boundary ) , ( 'Content-Disposition: form-data; name="%s"' % key ) , '' , str ( value ) ] ) lines . extend ( [ ( ( '--' + boundary ) + '--' ) , '' ] ) return '\r\n' . join ( lines )
like stream_encode_multipart but returns a tuple in the form where data is a bytestring .