Discussion:
Uploading a file using POST
(too old to reply)
Thomas Robitaille
2009-03-19 00:50:03 UTC
Permalink
Hello,

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.

At the moment I am trying

URL="http://www.whatever.com"
f=open(filename, 'rb')
filebody = f.read()
f.close()
data = {'name':'userfile','file': filebody}
u = urllib.urlopen(URL,urllib.urlencode(data))
u.read()

The page on the web server receiving the file is a PHP script. What I
would like is for the path to the uploaded file to appear in the
$_FILES global variable. However, this is not happening. Instead, the
content of the file is all contained inside the $_POST variable.

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>

which does result in the file being uploaded and the path placed in
$_FILES.

What am I doing wrong?

Thanks for any help!

Thomas
Justin Ezequiel
2009-03-19 02:20:44 UTC
Permalink
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 seen http://code.activestate.com/recipes/146306/
Vijayendra Bapte
2009-03-19 12:09:14 UTC
Permalink
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
Loading...