How to handle URLs errors in python using urllib
#!/usr/bin/python
from urllib import request
from urllib.error import HTTPError, URLError
uri = 'http://127.0.0.1:8000/'
req = request.Request(uri)
def Except():
try:
page = request.urlopen(req)
res = page.read()
print('It is ok')
except HTTPError as e:
print("HTTP error")
except URLError as e:
print("URL error")
except TypeError as e:
print("Type error")
except ValueError as e:
print("Value error")
if __name__ == "__main__":
Except()