Can't assign object * IO () to WRITEDATA, pycurl

Anyone else noticed that the pycurl example doesn't work in Python 2. *?

import pycurl
from StringIO import StringIO

buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://pycurl.sourceforge.net/')
c.setopt(c.WRITEDATA, buffer)
c.perform()
c.close()

body = buffer.getvalue()
# Body is a string in some encoding.
# In Python 2, we can print it without knowing what the encoding is.
print(body)

      

Then I get a failure like this

Traceback (most recent call last):
  File "./get_python2.py", line 9, in <module>
    c.setopt(c.WRITEDATA, buffer)
TypeError: invalid arguments to setopt

      

The assignment of WRITEFUNCTION and others seems to function as declared. Does anyone know what's going on here?

+3


source to share


2 answers


I think the docs indicates what you should use WRITEFUNCTION

if you don't have a true file object:

In Python 3 and Python 2, when the value is not a true file object, WRITEDATA is emulated in PycURL via WRITEFUNCTION.

So, you will need to use:

c.setopt(c.WRITEFUNCTION, buffer.write)

      



Edit:

PycURL Quickstart uses WRITEDATA as an example with StringIO, but this requires PycURL> = version 7.19.3:

As of PycURL 7.19.3 WRITEDATA accepts any Python object with a write method

+6


source


Try c.setopt (c.WRITEFUNCTION, buffer.write)



I had exactly the same problem and it worked. pycurl (7.19.0)

0


source







All Articles