Search code examples
pythoncurllibcurlpycurlattributeerror

pycurl: RETURNTRANSFER option doesn't exist


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.


Solution

  • 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">RFC
    
      2606</a>, Section 3.</p>
    </BODY>
    </HTML>
    

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