Uploading a file using POST

Vijayendra Bapte vijayendra.bapte at gmail.com
Thu Mar 19 08:09:14 EDT 2009


On Mar 19, 7:20 am, Justin Ezequiel <justin.mailingli... at gmail.com>
wrote:
> On Mar 19, 8:50 am, Thomas Robitaille <thomas.robitai... at gmail.com>
> wrote:
>
> > I am trying to upload a binary file (a tar.gz file to be exact) to a  
> > web server using POST, from within a python script.
>
> > What I would like is essentially the equivalent of
>
> > <form name='proceedings' method='post' action='www.whatever.com' 
> > enctype="multipart/form-data">
> > <input name="userfile" type="file">
> > </form>
>
> have you seenhttp://code.activestate.com/recipes/146306/

or you can use pycurl

>>> import pycurl
>>> c = pycurl.Curl()
>>> c.setopt(pycurl.URL, "http://www.whatever.com")
>>> c.setopt(pycurl.POST, 1)
>>> filepath = "/path/of/file"
>>> filename = "filename"
>>> c.setopt(pycurl.HTTPPOST, [('userfile', (pycurl.FORM_FILE, file_path, pycurl.FORM_FILENAME, filename))])
>>> c.perform()

read pycurl doc for more details. http://pycurl.sourceforge.net/doc/pycurl.html




More information about the Python-list mailing list