user926958
user926958

Reputation: 9565

python urllib2 library simple http_error_default error handler

This is how my class looks like:

class ExtHTTPErrorProcessor (urllib2.BaseHandler):

def http_error_default(self, req, fp, code, msg, hdrs):
    print code
    return res
...
...
opener = urllib2.build_opener(ExtHTTPErrorProcessor())
urllib2.install_opener(opener)
urllib2.urlopen(request, data)

All it does is to handle all error code. I follow exactly format from the documentation: http://docs.python.org/library/urllib2.html#urllib2.BaseHandler.http_error_default When error code 400 comes in, it does not call my http_error_default at all, it is calling the one inside urllib2. File "/usr/local/lib/python2.7/urllib2.py", line 521, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: Bad Request

I tried adding the method http_error_400, then this method will be called, but I really need the http_error_default. I really need a default one.

Upvotes: 1

Views: 1687

Answers (1)

Cees Timmerman
Cees Timmerman

Reputation: 19644

According to this vague old thread you must specify explicit handlers in the "Fancy" URL Opener... I'm sticking with my simple working solution. Just add a try ... except block.

Upvotes: 1

Related Questions