beginner_user beginner_user - 2 months ago 7
Python Question

How to check HTTP errors for more than two URLs?

Question: I've 3 URLS - testurl1, testurl2 and testurl3. I'd like to try testurl1 first, if I get 404 error then try testurl2, if that gets 404 error then try testurl3. How to achieve this? So far I've tried below but that works only for two url, how to add support for third url?

from urllib2 import Request, urlopen
from urllib2 import URLError, HTTPError

def checkfiles():
req = Request('http://testurl1')
try:
response = urlopen(req)
url1=('http://testurl1')

except HTTPError, URLError:
url1 = ('http://testurl2')

print url1
finalURL='wget '+url1+'/testfile.tgz'

print finalURL

checkfiles()

wim wim
Answer

Another job for plain old for loop:

for url in testurl1, testurl2, testurl3
    req = Request(url)
    try:
        response = urlopen(req)
    except HttpError as err:
        if err.code == 404:
            continue
        raise
    else:
        # do what you want with successful response here (or outside the loop)
        break
else:
    # They ALL errored out with HTTPError code 404.  Handle this?
    raise err
Comments