[Web-SIG] file uploads

Bill Janssen janssen at parc.com
Thu Oct 23 22:08:29 EDT 2003


> I certainly think a function for doing file uploads would be great,
> though.

It's not difficult.

I adapted this code from a version in the Python cookbook, by Wade Leftwich:

import httplib, mimetypes

def https_post_multipart(host, port, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    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 the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTPS(host, port)
    h.putrequest('POST', selector)
    h.putheader('Content-Type', content_type)
    h.putheader('Content-Length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return errcode, errmsg, headers, h.file.read()

def http_post_multipart(host, port, password, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    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 the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTP(host, port)
    h.putrequest('POST', selector)
    if password:
        h.putheader('Password', password)
    h.putheader('Content-Type', content_type)
    h.putheader('Content-Length', str(len(body)))
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    return errcode, errmsg, headers, h.file.read()

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 = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for file in files:
        key = file[0]
        filename = file[1]
        if len(file) > 2:
            value = file[2]
        else:
            value = None
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, os.path.basename(filename)))
        L.append('Content-Type: %s' % get_content_type(filename))
        if value:
            L.append('')
            L.append(value)
        else:
            L.append('Content-Transfer-Encoding: binary')
            L.append('')
            fp = open(filename, 'r')
            L.append(fp.read())
            fp.close()
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

def get_content_type(filename):
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'




More information about the Web-SIG mailing list