Anyone else noticed that the pycurl example doesn't work on 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
Assigning WRITEFUNCTION & others seem to function as advertised. Anyone know what's going on here?
I think the docs kind of indicate you have to use WRITEFUNCTION
when you don't have a true file object:
On Python 3 and on Python 2 when the value is not a true file object, WRITEDATA is emulated in PycURL via WRITEFUNCTION.
So you'd need to use:
c.setopt(c.WRITEFUNCTION, buffer.write)
Edit:
The PycURL Quickstart uses WRITEDATA as an example with StringIO, but it requires PycURL >= version 7.19.3:
As of PycURL 7.19.3 WRITEDATA accepts any Python object with a write method
Try c.setopt(c.WRITEFUNCTION, buffer.write)
I had exactly the same problem and it worked this way. pycurl (7.19.0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With