Uploading a file using POST

Thomas Robitaille thomas.robitaille at gmail.com
Wed Mar 18 20:50:03 EDT 2009


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



More information about the Python-list mailing list