python requests set timeout

Python Requests Set Timeout

If you're making HTTP requests using Python Requests library, you may encounter some situations where the request may take too long to complete, or it may get stuck. In such cases, you'll want to set a timeout for your requests so that they don't block your program indefinitely. In this post, we'll discuss how to set timeout for your Python Requests.

Setting Timeout

The timeout parameter is used to set the timeout for your requests. This parameter is specified in seconds and can be set for both the connect and read timeouts. Here's how you can use it:


import requests

# Setting connect and read timeouts to 5 seconds
response = requests.get('http://example.com', timeout=(5, 5))
  

In the above example, we're setting both the connect and read timeouts to 5 seconds. If either the connection establishment or the reading of the response takes more than 5 seconds, an exception will be raised.

Timeout Exceptions

If a timeout occurs during a request, a requests.exceptions.Timeout exception will be raised. You can catch this exception and handle it appropriately. Here's an example:


import requests
from requests.exceptions import Timeout

try:
    response = requests.get('http://example.com', timeout=0.001)
except Timeout:
    print('The request timed out')
  

In the above example, we're setting the timeout to 0.001 seconds, which is a very short timeout. The request will most likely timeout, and we're catching the requests.exceptions.Timeout exception and printing a message.

Other Timeout Settings

In addition to setting the timeout for your requests, you can also specify the timeout for the whole session:


import requests

# Setting session timeout to 5 seconds
session = requests.Session()
session.timeout = 5
response = session.get('http://example.com')
  

In the above example, we're setting the timeout for the whole session to 5 seconds. This means that any requests made using this session will have a default timeout of 5 seconds.

Another way to specify the timeout for the whole session is to pass it as a parameter when creating the session:


import requests

# Creating a session with a timeout of 5 seconds
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=100, pool_maxsize=100, max_retries=20)
session.mount('http://', adapter)
session.mount('https://', adapter)
response = session.get('http://example.com', timeout=5)
  

In the above example, we're creating a session with a timeout of 5 seconds and also specifying some other adapters for the session.