Putting a pyCurl XML server response into a variable (Python)

crashsystems picture crashsystems · Nov 2, 2008 · Viewed 8.4k times · Source

I'm a Python novice, trying to use pyCurl. The project I am working on is creating a Python wrapper for the twitpic.com API (http://twitpic.com/api.do). For reference purposes, check out the code (http://pastebin.com/f4c498b6e) and the error I'm getting (http://pastebin.com/mff11d31).

Pay special attention to line 27 of the code, which contains "xml = server.perform()". After researching my problem, I discovered that unlike I had previously thought, .perform() does not return the xml response from twitpic.com, but None, when the upload succeeds (duh!).

After looking at the error output further, it seems to me like the xml input that I want stuffed into the "xml" variable is instead being printed to ether standard output or standard error (not sure which). I'm sure there is an easy way to do this, but I cannot seem to think of it at the moment. If you have any tips that could point me in the right direction, I'd be very appreciative. Thanks in advance.

Answer

Alon Swartz picture Alon Swartz · Feb 28, 2010

Using a StringIO would be much cleaner, no point in using a dummy class like that if all you want is the response data...

Something like this would suffice:

import pycurl
import cStringIO

response = cStringIO.StringIO()

c = pycurl.Curl()
c.setopt(c.URL, 'http://www.turnkeylinux.org')
c.setopt(c.WRITEFUNCTION, response.write)
c.perform()
c.close()

print response.getvalue()