urllib.error — Exception classes raised by urllib.request¶
The urllib.error module defines the exception classes for exceptions raised by urllib.request . The base exception class is URLError .
The following exceptions are raised by urllib.error as appropriate:
exception urllib.error. URLError ¶
The handlers raise this exception (or derived exceptions) when they run into a problem. It is a subclass of OSError .
The reason for this error. It can be a message string or another exception instance.
Changed in version 3.3: URLError has been made a subclass of OSError instead of IOError .
Though being an exception (a subclass of URLError ), an HTTPError can also function as a non-exceptional file-like return value (the same thing that urlopen() returns). This is useful when handling exotic HTTP errors, such as requests for authentication.
An HTTP status code as defined in RFC 2616. This numeric value corresponds to a value found in the dictionary of codes as found in http.server.BaseHTTPRequestHandler.responses .
This is usually a string explaining the reason for this error.
The HTTP response headers for the HTTP request that caused the HTTPError .
This exception is raised when the urlretrieve() function detects that the amount of the downloaded data is less than the expected amount (given by the Content-Length header). The content attribute stores the downloaded (and supposedly truncated) data.
How to Fix HTTPError in Python
The urllib.error.HTTPError is a class in the Python urllib library that represents an HTTP error. An HTTPError is raised when an HTTP request returns a status code that represents an error, such as 4xx (client error) or 5xx (server error).
HTTPError Attributes
The urllib.error.HTTPError class has the following attributes:
- code : The HTTP status code of the error.
- reason : The human-readable reason phrase associated with the status code.
- headers : The HTTP response headers for the request that caused the HTTPError .
What Causes HTTPError
Here are some common reasons why an HTTPError might be raised:
- Invalid or malformed request URL.
- Invalid or malformed request parameters or body.
- Invalid or missing authentication credentials.
- Server internal error or malfunction.
- Server temporarily unavailable due to maintenance or overload.
Python HTTPError Examples
Here are a few examples of HTTP errors in Python:
404 Not Found
import urllib.request import urllib.error try: response = urllib.request.urlopen('http://httpbin.org/status/404') except urllib.error.HTTPError as err: print(f'A HTTPError was thrown: ')
In the above example, an invalid URL is attempted to be opened using the urllib.request.urlopen() function. Running the above code raises an HTTPError with code 404:
A HTTPError was thrown: 404 NOT FOUND
400 Bad Request
import urllib.request try: response = urllib.request.urlopen('http://httpbin.org/status/400') except urllib.error.HTTPError as err: if err.code == 400: print('Bad request!') else: print(f'An HTTP error occurred: ')
In the above example, a bad request is sent to the server. Running the above code raises a HTTPError with code 400:
401 Unauthorized
import urllib.request import urllib.error try: response = urllib.request.urlopen('http://httpbin.org/status/401') except urllib.error.HTTPError as err: if err.code == 401: print('Unauthorized!') else: print(f'An HTTP error occurred: ')
In the above example, a request is sent to the server with missing credentials. Running the above code raises a HTTPError with code 401:
500 Internal Server Error
import urllib.request import urllib.error try: response = urllib.request.urlopen('http://httpbin.org/status/500') except urllib.error.HTTPError as err: if err.code == 500: print('Internal server error!') else: print(f'An HTTP error occurred: ')
In the above example, the server experiences an error internally. Running the above code raises a HTTPError with code 500:
How to Fix HTTPError in Python
To fix HTTP errors in Python, the following steps can be taken:
- Check the network connection and ensure it is stable and working.
- Check the URL being accessed and make sure it is correct and properly formatted.
- Check the request parameters and body to ensure they are valid and correct.
- Check whether the request requires authentication credentials and make sure they are included in the request and are correct.
- If the request and URL are correct, check the HTTP status code and reason returned in the error message. This can give more information about the error.
- Try adding error handling code for the specific error. For example, the request can be attempted again or missing parameters can be added to the request.
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Python errors easier than ever. Try it today!