How to upload a file with httplib?

John J. Lee jjl at pobox.com
Fri Jun 27 09:07:47 EDT 2003


"Hank Hu" <hankhu at 263.net> writes:

> I'm writing a prototype with python and need upload a zip file to a web
> server. Any idea?

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

At your own risk, since file upload is poorly tested ATM.  You'd need
this version:

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

IIRC, there's not yet a method on HTMLForm, so you need to use the
control directly:

import urllib2
from ClientForm import ParseResponse

forms = ParseResponse(urllib2.urlopen("http://www.example.com/"))
form = forms[0]
ctrl = form.find_control(type="file")
# note multi-file upload not implemented yet
# name and content_type args. to add_file are optional
ctrl.add_file(open("my.zip"), name="my.zip")
response2 = urllib.urlopen(form.click())


John




More information about the Python-list mailing list