python urllib2库简单http_error_default error hand

2024-06-28 14:32:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的班级是这样的:

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)

它所做的就是处理所有的错误代码。我完全遵循文档中的格式:http://docs.python.org/library/urllib2.html#urllib2.BaseHandler.http_error_default 当出现错误代码400时,它根本不调用我的http\u error\u default,它调用的是urllib2中的错误代码。 文件“/usr/local/lib/python2.7/urllib2.py”,第521行,http\u error\u default 引发HTTPError(请求获取完整的网址(),代码、消息、hdrs、fp) urllib2.HTTPError:HTTP错误400:错误请求

我尝试添加方法http_error_400,然后将调用此方法,但我确实需要http_error_default。我真的需要一个默认的。在


Tags: 方法httpdefault错误codeerroropenerurllib2