Trace / BPT hook when calling urllib.urlopen

For some reason I get Trace / BPT trap error when called urllib.urlopen

. I tried both urllib

and urllib2

with the same results. Here is the code that raises 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 must add that this code is running on CherryPy web server with web.py.

Someone has requested a trace. Unfortunately, they are not. Trace/BPT trap

is output to the terminal and the process ends. For example.

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.

+2


source to share


2 answers


Adding the following lines to the beginning of the main file resolved the issue:

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

      



In other words, it's not enough to import the urllib2 module, but you really need to create an opener on the main thread.

+3


source


Does this work under OS X 10.6? Obviously streams and module imports for the first time don't go well there. Make sure you can't import urllib2 outside of the stream?

The following thread has a few details: Trace / BPT Trap with Python Streaming Module

I will try to either move the urllib import to the top of the same file, or since this only seems to be a problem the first time you enter a module in a thread, import it somewhere else, for example in the same file as your main () function.



Edit: What versions of OS X, Python, CherryPy, and web.py are you using? I am using OS X 10.5.8, Python 2.6, CherryPy 3.1.2 and web.py 0.33 and cannot reproduce your problem using the code below:

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 sufficient to reproduce the problem at your end? If not, I need more information to help.

+2


source







All Articles