重定向次数比rediection_limit允许的次数多 - python / Django

时间:2014-09-13 11:30:01

标签: python django

我试图通过这样做来检查URL是否是有效的URL:

def check_urlstatus(url):
    h = httplib2.Http()
    try:
       resp = h.request("http://" + url, 'HEAD')        
       if int(resp[0]['status']) < 400:
           return 'ok'
       else:
           return 'bad'
    except (httplib2.ServerNotFoundError, UnicodeError, httplib2.RelativeURIError):
       return 'bad'

但有些网址即使有效也似乎无法通过。例如这一个:www.healthpolicyjrnl.com

我收到错误:

Redirected more times than rediection_limit allows.

我怎么能抓住这个错误?我会为此返回bad

第二个问题是: 我错过了except中应该捕获的任何其他潜在错误吗?

1 个答案:

答案 0 :(得分:1)

您可以在httplib2 source和自定义RedirectLimit例外中看到如何引发此异常。所以抓住它:

from httplib2 import RedirectLimit

try:
   ...
except (RedirectLimit, httplib2.ServerNotFoundError, UnicodeError, httplib2.RelativeURIError):
   return 'bad'
相关问题