In the Python requests
library, managing timeouts is crucial to ensure that your code doesn't hang indefinitely if a server is slow to respond or if there are network issues. The requests
library allows you to specify timeouts as a parameter to the request functions (get
, post
, put
, delete
, etc.).
Here's how you can manage timeouts:
Setting a Timeout
You can set a timeout by passing a number of seconds to the timeout
parameter:
import requests
try:
response = requests.get('https://example.com', timeout=5) # Timeout is 5 seconds
except requests.Timeout:
print('The request timed out')
except requests.RequestException as e:
print('An error occurred:', e)
In this example, if the server has not sent a response within 5 seconds, a requests.Timeout
exception will be raised.
Timeout for Connect and Read
The timeout
parameter can also be a tuple (connect_timeout, read_timeout)
:
import requests
try:
# Connect timeout is 3 seconds, read timeout is 5 seconds
response = requests.get('https://example.com', timeout=(3, 5))
except requests.Timeout:
print('The request timed out')
except requests.RequestException as e:
print('An error occurred:', e)
Here, connect_timeout
is the number of seconds Requests will wait for your client to establish a connection to a server, and read_timeout
is the number of seconds the client will wait for the server to send a response.
Infinite Timeout
If you don't set a timeout, your request could potentially hang indefinitely. By default, requests
does not set a timeout; unless you explicitly specify one, there will be no timeout:
import requests
# No timeout is specified
response = requests.get('https://example.com')
Global Timeout Default
If you want to set a global default timeout for all requests in your application, you can subclass requests.Session
and override the request
method:
import requests
class TimeoutSession(requests.Session):
def request(self, *args, **kwargs):
kwargs.setdefault('timeout', 5) # Set a default timeout for all requests
return super().request(*args, **kwargs)
session = TimeoutSession()
response = session.get('https://example.com') # This request will use the default timeout of 5 seconds
Best Practices
- Always set a timeout. The risk of leaving your code hanging indefinitely is usually not acceptable in a production environment.
- Choose a reasonable timeout value based on your specific use case. For example, if you're making requests to a service that you know is quick, a shorter timeout could be appropriate.
- Handle exceptions properly. Make sure to catch
requests.Timeout
errors to handle them gracefully in your code.
Keep in mind that some requests might take longer due to large file transfers or slow server responses. In such cases, you might need to adjust your timeout settings accordingly.