调用 urllib.urlopen 时的 Trace/BPT 陷阱

发布于 2024-08-09 10:44:33 字数 687 浏览 7 评论 0原文

由于某种原因,我在调用 urllib.urlopen 时收到 Trace/BPT 陷阱错误。我尝试了 urlliburllib2 ,结果相同。以下是引发错误的代码:

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。

For some reason I'm getting a Trace/BPT trap error when calling urllib.urlopen. I've tried both urllib and urllib2 with identical results. Here is the code which throws the error:

def get_url(url):
    from urllib2 import urlopen
    if not url or not url.startswith('http://'): return None
    return urlopen(url).read() # FIXME!

I should add that this code is running on a CherryPy webserver with web.py.

Someone requested a traceback. Unfortunately, there is none. Trace/BPT trap is outputted to the terminal and the process terminates. E.g.

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 $

Edit: I am running OS X 10.6.2, web.py 0.33, Python 2.6.2, and CherryPy 3.1.2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-08-16 10:44:33

在主文件顶部添加以下几行解决了问题:

import urllib2
urllib2.install_opener(urllib2.build_opener())

换句话说,导入 urllib2 模块还不够,您实际上需要在主线程中创建 opener。

Adding the following lines to the top of the main file solved the problem:

import urllib2
urllib2.install_opener(urllib2.build_opener())

In other words, it is not enough to import the urllib2 module but you actually need to create the opener in the main thread.

少年亿悲伤 2024-08-16 10:44:33

您在 OS X 10.6 下运行吗?显然,线程和导入模块第一次并不能很好地协同工作。看看线程外是否不能导入urllib2?

以下线程中有更多详细信息: Trace/BPT trap with Python threading module

我会尝试将 urllib 的导入移动到同一文件的顶部,或者因为这似乎只是在线程中第一次导入模块时出现问题,所以也将其导入其他地方,就像在与 main() 函数相同的文件中。

编辑:您正在运行哪些版本的 OS X、Python、CherryPy 和 web.py?我使用的是 OS X 10.5.8、Python 2.6、CherryPy 3.1.2 和 web.py 0.33,无法使用以下代码重现您的问题:

import web

urls = (
  '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        from urllib2 import urlopen
        return urlopen("http://google.se/").read()

if __name__ == "__main__": app.run()


$ sudo python index.py 80
http://0.0.0.0:80/
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /" - 200 OK
127.0.0.1:59604 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /extern_js/f/CgJzdhICc2UgACswCjhBQB0sKzAOOAksKzAYOAQsKzAlOMmIASwrMCY4BSwrMCc4Aiw/dDWkSd2jmF8.js" - 404 Not Found
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /logos/elmo-hp.gif" - 404 Not Found
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /images/nav_logo7.png" - 404 Not Found

此代码是否足以重现您的问题?如果没有,我需要更多信息才能提供帮助。

Are you running this under OS X 10.6? Apparently threads and importing modules for the first time does not play well together there. See if you can't import urllib2 outside of the thread?

There are a few more details in the following thread: Trace/BPT trap with Python threading module

I'd try either moving the import of urllib to the top of the same file or, since it seems to be a problem only with importing a module for the first time in a thread, import it somewhere else as well, like in the same file as your main() function.

Edit: Which versions of OS X, Python, CherryPy and web.py are you running? I'm using OS X 10.5.8, Python 2.6, CherryPy 3.1.2 and web.py 0.33 and can't reproduce your problem using the below code:

import web

urls = (
  '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        from urllib2 import urlopen
        return urlopen("http://google.se/").read()

if __name__ == "__main__": app.run()


$ sudo python index.py 80
http://0.0.0.0:80/
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /" - 200 OK
127.0.0.1:59604 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /extern_js/f/CgJzdhICc2UgACswCjhBQB0sKzAOOAksKzAYOAQsKzAlOMmIASwrMCY4BSwrMCc4Aiw/dDWkSd2jmF8.js" - 404 Not Found
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /logos/elmo-hp.gif" - 404 Not Found
127.0.0.1:59601 - - [08/Nov/2009 09:46:40] "HTTP/1.1 GET /images/nav_logo7.png" - 404 Not Found

Is this code enough to reproduce the problem on your end? If not, I need more information in order to be of help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文