(fields, files)
return content_type, body
fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
fields is a sequence of ( name value ) elements for regular form fields . files is a sequence of ( name filename value ) elements for data to be uploaded as files Return ( content_type body ) ready for httplib . HTTP instance
def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = b'\r\n'
    L = []
    for (key, value) in fields:
        L.append(b'--' + BOUNDARY)
        L.append(bytes('Content-Disposition: form-data; name="%s"' % key, 'ascii'))
        L.append(b'')
        L.append(value)
    for (key, filename, value) in files:
        L.append(b'--' + BOUNDARY)
        L.append(bytes('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename), 'ascii'))
        L.append(bytes('Content-Type: %s' % mimetypes.guess_type(filename)[0] or 'application/octet-stream', 'ascii'))
        L.append(b'')
        L.append(value)
    L.append(b'--' + BOUNDARY + b'--')
    L.append(b'')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY.decode('ascii')
    return content_type, body
def encode_multipart_formdata ( fields , files ) : BOUNDARY = b'----------ThIs_Is_tHe_bouNdaRY_$' CRLF = b'\r\n' L = [ ] for ( key , value ) in fields : L . append ( b'--' + BOUNDARY ) L . append ( bytes ( 'Content-Disposition: form-data; name="%s"' % key , 'ascii' ) ) L . append ( b'' ) L . append ( value ) for ( key , filename , value ) in files : L . append ( b'--' + BOUNDARY ) L . append ( bytes ( 'Content-Disposition: form-data; name="%s"; filename="%s"' % ( key , filename ) , 'ascii' ) ) L . append ( bytes ( 'Content-Type: %s' % mimetypes . guess_type ( filename ) [ 0 ] or 'application/octet-stream' , 'ascii' ) ) L . append ( b'' ) L . append ( value ) L . append ( b'--' + BOUNDARY + b'--' ) L . append ( b'' ) body = CRLF . join ( L ) content_type = 'multipart/form-data; boundary=%s' % BOUNDARY . decode ( 'ascii' ) return content_type , body