Search code examples
pythonxmlpycurl

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


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.


Solution

  • The pycurl doc explicitly says:

    perform() -> None

    So the expected result is what you observe.

    looking at an example from the pycurl site:

    import sys
    import pycurl
    
    class Test:
       def __init__(self):
           self.contents = ''
    
       def body_callback(self, buf):
           self.contents = self.contents + buf
    
    print >>sys.stderr, 'Testing', pycurl.version
    
    t = Test()
    c = pycurl.Curl()
    c.setopt(c.URL, 'http://curl.haxx.se/dev/')
    c.setopt(c.WRITEFUNCTION, t.body_callback)
    c.perform()
    c.close()
    
    print t.contents
    

    The interface requires a class instance - Test() - with a specific callback to save the content. Note the call c.setopt(c.WRITEFUNCTION, t.body_callback) - something like this is missing in your code, so you do not receive any data (buf in the example). The example shows how to access the content:

    print t.contents