using urllib or httplib to post with ENCTYPE=mulitpart/form-data

John J. Lee jjl at pobox.com
Thu Jun 5 01:58:09 CEST 2003


Kevin Carlson <khcarlso at bellsouth.net> writes:
[...]
> I am trying to post to a form using httplib or urllib.  I have done
> this successfully before with httplib, but when the enctype must be
> multipart/form-data, things go awry.
> 
> I tried using a header {'Content-Type' : 'multipart/form-data', ...}
> that I encode with urlencode and then pass to the
> HTTPConnection.request method, but the server is returning a code
> indicating a bad request.  I have done a lot of troubleshooting on
> this and the encoding type is the only remaining issue.
> 
> Can anyone shed some light on how to accomplish a POST of this type?

Erm, I'm hesitant to suggest this module, because the version that
does INPUT TYPE=FILE file upload -- which I'm guessing is what you
want to do -- is currently still in alpha (I think I've only tested it
against the cgi module and it only does single-file upload -- not
multiple files -- and internally it's clunky, too).  But if it doesn't
work, tell me and I'll likely fix it quickly.

Anyway (using urllib2 here for convenience, but you can use urllib
too, with minor changes), untested:

from urllib2 import urlopen
from ClientForm import ParseResponse

forms = ParseResponse(urlopen("http://blah.com/"))
form = forms[0]
# do some form filling, eg.
form["uname"] = "bob"
form["pwd"] = "XXX"
# add file to upload
f = open("/some/file.txt")
form.add_file(f, content_type="text/plain", name="file.txt")

# pass name to click method if there's more than one button
response = urlopen(form.click())

# urllib2's responses are file objects with some extra stuff:
print response2.geturl()  # url
print response2.info()  # headers
print response2.read():  # body (readline, readlines also work IIRC)


you'd need this:

http://wwwsearch.sourceforge.net/ClientForm/src/ClientForm-0.1.1a.tar.gz

home page of code:

http://wwwsearch.sourceforge.net/ClientForm/

HTH


John




More information about the Python-list mailing list