1 from urllib.request import Request, urlopen
2 from urllib.error import URLError, HTTPError
3 req = Request(someurl)
4 try:
5 response = urlopen(req)
6 except HTTPError as e:
7 print('The server couldn\'t fulfill the request.')
8 print('Error code: ', e.code)
9 except URLError as e:
10 print('We failed to reach a server.')
11 print('Reason: ', e.reason)
12 else:
13 # everything is fine
1 from urllib.request import Request, urlopen
2 from urllib.error import URLError
3 req = Request(someurl)
4 try:
5 response = urlopen(req)
6 except URLError as e:
7 if hasattr(e, 'reason'):
8 print('We failed to reach a server.')
9 print('Reason: ', e.reason)
10 elif hasattr(e, 'code'):
11 print('The server couldn\'t fulfill the request.')
12 print('Error code: ', e.code)
13 else:
14 # everything is fine