FAQ
Hi,
I use httplib to automatically filled in some fields in web page forms.
I use a similar program like the example in Python DOC that is
import httplib, urllib
params = "spam=1&eggs=2&bacon=0"
h = httplib.HTTP("www.musi-cal.com:80")
h.putrequest("POST", "/cgi-bin/query")
h.putheader("Content-type", "application/x-www-form-urlencoded")
h.putheader("Content-length", "%d" % len(params))
h.putheader('Accept', 'text/plain')
h.putheader('Host', 'www.musi-cal.com')
h.endheaders()
h.send(params)

So, as you can see params are
name1=Value1&name2=Value2&....nameN=ValueN
My question is:
If a form uses
"multipart/form-data" instead of
"application/x-www-form-urlencoded"
in which form params (data) must be?
Thanks for help
Ladislav

Search Discussions

  • Thomas Guettler at May 3, 2003 at 12:00 pm
    On Sat, May 03, 2003 at 11:52:09AM +0200, A wrote:
    [cut]
    If a form uses
    "multipart/form-data" instead of
    "application/x-www-form-urlencoded"
    in which form params (data) must be?
    In the python cookbook there is a recipie. Here it is:


    #The following functions are from the python cookbook
    def post_multipart(host, selector, fields, files, user, password):
    """
    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)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.putheader("AUTHORIZATION", "Basic %s" % string.replace(
    base64.encodestring("%s:%s" % (user, password)),
    "\012", ""))
    h.putheader('User-agent', 'uploadclient')
    h.endheaders()
    h.send(body)
    errcode, errmsg, headers = h.getreply()
    print "%s: Upload of http://%s%s %s errcode: %s errmsg: %s headers: %s" % (
    time.strftime("%Y-%m-%d %H:%M:%S"),
    host, selector, files[0][1],
    errcode, errmsg, headers)

    return 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 (key, filename, value) in files:
    L.append('--' + BOUNDARY)
    L.append('Content-Disposition: form-data; name="%s"; filename="%s"' %
    (key, filename))
    L.append('Content-Type: %s' % get_content_type(filename))
    L.append('')
    L.append(value)
    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'
    #End of code from python cookbook



    --
    Thomas Guettler <guettli at thomas-guettler.de>
    http://www.thomas-guettler.de

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 3, '03 at 9:52a
activeMay 3, '03 at 12:00p
posts2
users2
websitepython.org

2 users in discussion

Thomas Guettler: 1 post A: 1 post

People

Translate

site design / logo © 2022 Grokbase