First, no matter what I did, I had to save my file to the server by using the following line of code:
open(randomimagename, 'wb').write(f['image'].file.read())
People that want to use pycurl should be able to send a response to teh server by using the following code. However, this code did not work on my server and will need to be changed in order to get it to work:
import pycurl
pf = [('submituser', f['submituser']), ('tags', f['tags']), ('description', f['description']), ('image', (pycurl.FORM_FILE, randomimagename)) ]
c = pycurl.Curl()
c.setopt(c.URL, "http://www.python.org/")
c.setopt(c.POST, 1)
c.setopt(c.HTTPPOST
c.perform()
c.close()
You can get the response from pycurl by using the following code:
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
This code goes before you make the perform() call.
This line goes after you make the perform() call.
print b.getvalue()
However, this approach did not work for me. I ended up just making a system call to the curl command. This command works about the same way. Here's what I did:
import os;
os.system('curl -ivvv -X POST -F submituser=DavidW -F description=coolness -F
tags=foo -F image=@yellow_corvette.jpg
http://imaj-app.lddi.org:8010/submitimage')
Of course, things may need to be changed in this line to make it work for the actual server, but I was able to change the variables and customize them to get tehm to work for my specific case.
No comments:
Post a Comment