出于某种原因,我在调用 urllib.urlopen
时收到 Trace/BPT 陷阱错误。我尝试了 urllib
和 urllib2
,结果相同。这是引发错误的代码:
def get_url(url):
from urllib2 import urlopen
if not url or not url.startswith('http://'): return None
return urlopen(url).read() # FIXME!
我应该补充一点,此代码正在带有 web.py 的 CherryPy Web 服务器上运行。
有人请求回溯。不幸的是,没有。 Trace/BPT trap
输出到终端,进程终止。例如
dloewenherz@andros project $ sudo ./index.py 80
http://0.0.0.0:80/
# Here I visit the page which contains the get_url(url) method
Trace/BPT trap
dloewenherz@andros project $
编辑:我正在运行 OS X 10.6.2、web.py 0.33、Python 2.6.2 和 CherryPy 3.1.2。
最佳答案
将以下行添加到主文件顶部解决了问题:
import urllib2
urllib2.install_opener(urllib2.build_opener())
换句话说,仅仅导入 urllib2 模块是不够的,你实际上需要在主线程中创建 opener。
关于python - 调用 urllib.urlopen 时的 Trace/BPT 陷阱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1628916/