Timeout and Error Handling in Requests
When working with web requests, it is essential to manage timeouts and handle potential errors to make your application robust and reliable. The Requests library in Python provides mechanisms to set timeouts and catch exceptions effectively.
Setting Timeouts for Requests
Setting a timeout ensures that your program does not hang indefinitely while waiting for a response from the server. Use the timeout
parameter to specify a maximum wait time for the request.
Example: Setting a Timeout
import requests
url = "https://httpbin.org/delay/5" # Simulates a delay of 5 seconds
try:
response = requests.get(url, timeout=3) # Timeout set to 3 seconds
print("Response received:", response.text)
except requests.exceptions.Timeout:
print("The request timed out!")
Output (if timeout occurs)
The request timed out!
Handling Exceptions
Requests can raise several exceptions, depending on the situation. Catching these exceptions ensures your program handles errors gracefully.
Common Exceptions
requests.exceptions.RequestException
: Base class for all exceptions in Requests.requests.exceptions.Timeout
: Raised when a request times out.requests.exceptions.ConnectionError
: Raised for network-related errors.requests.exceptions.HTTPError
: Raised for HTTP-specific errors, such as 4xx or 5xx responses.
Example: Catching Specific Exceptions
url = "https://example.com/nonexistent"
try:
response = requests.get(url, timeout=5)
response.raise_for_status() # Raises HTTPError for bad responses (4xx and 5xx)
print("Response received:", response.text)
except requests.exceptions.Timeout:
print("The request timed out!")
except requests.exceptions.ConnectionError:
print("A connection error occurred!")
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except requests.exceptions.RequestException as err:
print(f"An error occurred: {err}")
Using raise_for_status()
for HTTP Errors
The raise_for_status()
method raises an HTTPError if the HTTP response contains a 4xx or 5xx status code.
Example: Handling HTTP Errors
url = "https://httpbin.org/status/404" # Simulates a 404 error
try:
response = requests.get(url)
response.raise_for_status()
print("Response received:", response.text)
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
Try It Yourself
Problem 1: Timeout Handling
Send a GET request to https://httpbin.org/delay/10
with a timeout of 5 seconds. Handle the timeout exception gracefully.
Show Solution
import requests
url = "https://httpbin.org/delay/10"
try:
response = requests.get(url, timeout=5)
print("Response received:", response.text)
except requests.exceptions.Timeout:
print("The request timed out!")
Problem 2: Catching HTTP Errors
Send a GET request to https://httpbin.org/status/500
and handle the HTTPError exception.
Show Solution
import requests
url = "https://httpbin.org/status/500"
try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
Setting timeouts and handling exceptions effectively is crucial for creating reliable and user-friendly applications. Practice these techniques to build robust Python programs with the Requests library!