5

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?

user1906583
  • 115
  • 2
  • 9

2 Answers2

10

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

Gerrat
  • 28,863
  • 9
  • 73
  • 101
  • I guess this is it. But this example is taken from PyCURL Quick Start... . Anyways, thanks for pointing this out, have an upvote! – user1906583 Jan 08 '15 at 18:31
  • 1
    @user1906583: Interesting...which versions of PycURL & Python are you using? – Gerrat Jan 08 '15 at 18:40
  • Someone wants to debug? I'm using Python 2.7.8 on Cygwin, installed from Cygwin's package mangement-thingy. Then I just downloaded pycurl 7.19.5 using "python setup.py install". The above example comes from http://pycurl.sourceforge.net/doc/quickstart.html, first block of code. BTW you were right, c.setopt(c.WRITEFUNCTION, buffer.write) works if I instantiate buffer = BytesIO(). – user1906583 Jan 08 '15 at 21:13
  • 1
    @user1906583: The reason I ask is that your example works for me (no exceptions). Maybe you've installed pycurl in a different python instance than what you're actually running. If you print out `pycurl.version` in your program, does it show that version (7.19.5)? – Gerrat Jan 08 '15 at 21:19
  • hello Gerrat. Here's a string that returned "'libcurl/7.39.0 OpenSSL/1.0.1j zlib/1.2.8 libidn/1.29 libssh2/1.4.2'". So it seems like I'm using older than 7.19.3 of pycurl. This means my "python setup.py install" didn't work properly. Too bad I didn't keep a log of this install. But good to know that I have a much deeper problem now! – user1906583 Jan 08 '15 at 22:18
1

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

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

Sart
  • 11
  • 2