Scripting HTTP POST - multipart/form-data

J

Jeff Shannon

I'm looking to script a routine file-upload through an HTTP server
(which I don't have access to). I've got specs on the CGI app that
receives this upload (i.e. field names and expected contents).

However, it looks like urllib/urllib2 won't handle multipart/form-data
POSTs. I tried hacking together something with httplib, but I'm
severely hampered by the fact that I don't know HTTP well enough to know
what headers I need to send, and my attempts to find (through Google) a
list of necessary headers haven't turned out too well. I really don't
want to have to scour the entire HTTP specs for this...

At a quick glance, it seems that PyCURL may do what I need, but it's...
well, not exactly user-friendly (at least for someone with zero
experience with libcurl), and the machine I'm trying to send from (RH9)
doesn't have libcurl installed. (I'd prefer to not have to install
anything just for this, if I can avoid it...)

Is there another (preferably all-python) library that will work for
this? Or perhaps a friendly how-to that'll explain what I need to know
to use httplib?

Jeff Shannon
Technician/Programmer
Credit International
 
J

John J. Lee

Jeff Shannon said:
I'm looking to script a routine file-upload through an HTTP server
(which I don't have access to). I've got specs on the CGI app that
receives this upload (i.e. field names and expected contents).

However, it looks like urllib/urllib2 won't handle multipart/form-data
POSTs.

Neither should have any problem with multipart/form-data. They don't
actually provide and specific support for it either, though.

[...]
Is there another (preferably all-python) library that will work for
this?
[...]

Yes, one that works on top of urllib2:

http://wwwsearch.sf.net/ClientForm


Search for .add_file on that page for an example of file upload. IIRC
I've posted examples of that here in the past, too.


John
 
J

John J. Lee

Jeff Shannon said:
I'm looking to script a routine file-upload through an HTTP server
(which I don't have access to). I've got specs on the CGI app that
receives this upload (i.e. field names and expected contents).
[...]

Forgot to add: if I had to do this and wanted to use ClientForm (which
I certainly would ;-), I'd just write a snippet of HTML:

formHtml = """\
<form action="http://www.example.com/cgi-bin/spam.cgi">
<input type="text" name="nrEmails" />

<input type="checkbox" name="spamType" value="ludicrous" />
<input type="checkbox" name="spamType" value="absurd" />
</form>
"""

import StringIO
from ClientForm import ParseFile

forms = ParseFile(StringIO.StringIO(formHtml), "http://www.example.com/")
....


Which is surely a very readable way of writing down such a spec :)

I should point this out in the docs...


John
 
J

Jeff Shannon

John said:
Neither should have any problem with multipart/form-data. They don't
actually provide and specific support for it either, though.

True. I realized that, I was merely inexact with my wording. ;) The
problem being that I don't know how to enhance them to add that support...
[...]

Is there another (preferably all-python) library that will work for
this?
[...]

Yes, one that works on top of urllib2:

http://wwwsearch.sf.net/ClientForm

Ah, thank you! That looks like just what I needed.

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top