Uploading a file using POST

T

Thomas Robitaille

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
 
V

Vijayendra Bapte

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top