File upload using httplib

Jeff Shannon jeff at
Tue Jul 27 01:45:46 CEST 2004

alastair wrote:

>I'm attempting to test out some functionality of the Apache http
>server. What I'd like to do is send a file to the server - eg. a text
>file or binary file (I will be testing gzipped transfers eventually

Keep in mind that, in order to send a file, you must use a content type 
of multipart/form-data, rather than your current 
application/x-www-form-urlencoded.  Neither urllib nor httplib directly 
support multipart/form-data, but (as someone else pointed out when I 
asked about this very recently) there's a recipe for it in ActiveState's 
Python Cookbook:

Note that the recipe uses an older httplib.HTTP() interface rather than 
the newer (and preferred) httplib.HTTPConnection() interface.  However, 
the only change necessary to use the new interface (beyond changing the 
inital call) is to change the h.getreply() to h.getresponse(), and 
handle the HTTPResponse object properly.

Jeff Shannon
Credit International

More information about the Python-list mailing list