views:

561

answers:

3

I'm using pycurl to access a JSON web API, but when I try to use the following:

ocurl.setopt(pycurl.URL, gaurl)       # host + endpoint
ocurl.setopt(pycurl.RETURNTRANSFER, 1)
ocurl.setopt(pycurl.HTTPHEADER, gaheader) # Send extra headers
ocurl.setopt(pycurl.CUSTOMREQUEST, "POST") # HTTP POST req
ocurl.setopt(pycurl.CONNECTTIMEOUT, 2)

and execute the script, it fails.

File "getdata.py", line 46, in apicall
ocurl.setopt(pycurl.RETURNTRANSFER, 1)
AttributeError: 'module' object has no attribute 'RETURNTRANSFER'

I haven't a clue what's going on, and why RETURNTRANSFER doesn't appear to exist while all the other options do.

A: 

Have you tried executing print dir(pycurl) and see if the option exists in the attribute list?

Nadia Alramli
+2  A: 

The manual shows the usage being something like this:

>>> import pycurl
>>> import StringIO
>>> b = StringIO.StringIO()
>>> conn = pycurl.Curl()
>>> conn.setopt(pycurl.URL, 'http://www.example.org')
>>> conn.setopt(pycurl.WRITEFUNCTION, b.write)
>>> conn.perform()
>>> print b.getvalue()
<HTML>
<HEAD>
  <TITLE>Example Web Page</TITLE>
</HEAD>
<body>
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,
  or &quot;example.org&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not availabl
e
  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt"&gt;RFC

  2606</a>, Section 3.</p>
</BODY>
</HTML>

Seems a little roundabout, but I'm not a big fan of PycURL...

Paolo Bergantino
Yeah, that works great. I wonder why they wouldn't just implement RETURNTRANSFER to start with.
Jonathan Prior
+1  A: 

CURLOPT_RETURNTRANSFER is not a libcurl option, it is but provided within the PHP/CURL binding

Daniel Stenberg