doniyor
doniyor

Reputation: 37886

Redirected more times than rediection_limit allows - python/Django

I am trying to check if a URL is valid URL by doing this:

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'

But some URLs dont seem to pass even if they are valid. e.g. this one: www.healthpolicyjrnl.com

I am getting the error:

Redirected more times than rediection_limit allows.

how can I catch this error? i would return bad for this.

second question is: am I missing any other potential Error which I should catch in except?

Upvotes: 0

Views: 612

Answers (1)

Timmy O&#39;Mahony
Timmy O&#39;Mahony

Reputation: 53981

You can see how this exception is raised in the httplib2 source and the custom RedirectLimit exception. So to catch it:

from httplib2 import RedirectLimit

try:
   ...
except (RedirectLimit, httplib2.ServerNotFoundError, UnicodeError, httplib2.RelativeURIError):
   return 'bad'

Upvotes: 1

Related Questions