python catch HTTP 502错误?

时间:2013-08-07 01:54:00

标签: python exception

我遇到了一些502错误,如果你收到一个或两次重试请求,我怎样才能使客户端能够抵御5XX错误?即使它们确实发生了这种情况,你的软件也会继续正常运行。

我的代码在http://pastebin.com/YHpZQ9Z9

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

try:
    # code that can potentially throw a 502
except HTTPError as e:
    if e.code == 502:
        #put your retry logic here
    else
        print 'Failure: ' + str(e.reason)

有关详情,请参阅urllib2.HTTPError docs