Sending pyCurl XML server response to a variable (Python)

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

Pay particular attention to line 27 of the code, which contains "xml = server.perform ()". Having studied my problem, I found that, as I already thought earlier, the .perform () function does not return the xml response from twitpic.com, but None when the download failed (duh!).

After looking at the error output further, it seems to me that the xml input that I want to add to the "xml" variable is instead printed to the standard standard output or standard error (not sure which one). I am sure there is an easy way to do this, but I can’t think about it now. If you have any advice that could point me in the right direction, I would really appreciate it. Thanks in advance.

+3
source share
2 answers

The pycurl doc explicitly states:

execute () → None

So, the expected result is what you are observing.

looking at an example from the pycurl website:

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

- Test() - . c.setopt(c.WRITEFUNCTION, t.body_callback) - - , (buf ). , :

print t.contents
+4

StringIO , -, , , ...

- :

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()
+12

Source: https://habr.com/ru/post/1699012/


All Articles