Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fail to assign *IO() object to WRITEDATA, pycurl

Tags:

python

pycurl

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?

like image 660
user1906583 Avatar asked Dec 26 '22 00:12

user1906583


2 Answers

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

like image 154
Gerrat Avatar answered Dec 27 '22 12:12

Gerrat


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

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

like image 24
Sart Avatar answered Dec 27 '22 14:12

Sart