In this short post, we will discuss the urllib.error
module that defines exception classes from the urllib.request module.
The module supports the following exceptions:
URLError
– This type of exception is raised when fetching a specific resource. The function provides a reason property which hold detailed information about the cause of the error.HTTPError
– This type of exception is raised in the encounter of exotic HTTP errors such as auth. Similarly, the function supports a code, reason, and headers properties that returns the HTTP status code, explanation of the error, and the HTTP response headers for the request, respectively.ContentTooShortError
– This exception is raised if the returned data is lass than expected amount. The data length is defined in the Content-Length header.
Example 1 – URLError Exception
The following example code shows how to use the errors raised in the errors module.
import urllib.request
try:
r = urllib.request.urlopen("https://cloudenv.io")
print(r.read().decode('utf-8')) # Print the response content as a string
except Exception as e:
print(str(e))
Keep in mind that the URLError
is a subclass of OSError
. Therefore, if we run the code above without internet connectivity, it should return a URLError as shown:
URL Error: urlopen error [Errno 11001] getaddrinfo failed
Example 2 - HTTPError Exception
In the example below, we illustrate how to use the urllb.error module to handle HTTPError exceptions.
import urllib.request
try:
r = urllib.request.urlopen("https://httpstat.us/403")
print(r.read().decode('utf-8'))
except Exception as e:
print(str(e))
Running the code above should return a HTTPError exception as the request encounters a 403
status code.
The resulting output:
HTTP Error 403: Forbidden
Closing
In this post, we discussed how to use the urllib.error module to handle URLError and HTTPError exceptions.