pycurl: RETURNTRANSFER选项不存在
我正在使用pycurl来访问一个JSON格式的网络API,但当我尝试使用以下代码时:
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)
然后执行这个脚本,它失败了。
File "getdata.py", line 46, in apicall
ocurl.setopt(pycurl.RETURNTRANSFER, 1)
AttributeError: 'module' object has no attribute 'RETURNTRANSFER'
我完全不知道发生了什么,为什么RETURNTRANSFER这个选项似乎不存在,而其他选项都正常。
3 个回答
0
你有没有试过运行 print dir(pycurl)
这个命令,看看在属性列表里有没有这个选项?
5
CURLOPT_RETURNTRANSFER 不是 libcurl 的选项,它只是 PHP 和 CURL 结合时提供的一个功能。
7
手册上显示的用法大概是这样的:
>>> 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 "example.com",
"example.net",
or "example.org" 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>
看起来有点绕,但我其实不太喜欢用PycURL……