python requests post timeout retry

Python Requests Post Timeout Retry

If you are working with APIs and sending HTTP requests using Python's Requests library, you may encounter situations where the server takes too long to respond or the connection times out. In such cases, it is useful to implement a retry mechanism to ensure that the request is sent again after a certain period of time until a response is received. This can be achieved using the 'timeout' and 'retry' parameters of the Requests library.

Timeout Parameter

The 'timeout' parameter is used to specify the maximum amount of time that the client should wait for a response from the server. If the server fails to respond within this time period, a 'Timeout' exception is raised. By default, the timeout value is set to 'None' which means that the client will wait indefinitely for a response.


import requests

url = 'https://example.com/api'

# Set timeout value to 5 seconds
timeout = 5

try:
    response = requests.post(url, timeout=timeout)
    print(response.status_code)
except requests.exceptions.Timeout:
    print("Request Timed Out")
  

Retry Parameter

The 'retry' parameter is used to specify the number of times that the request should be retried in case of a failure. This can be useful when the server is temporarily unavailable or if there are network issues. By default, the retry value is set to '0' which means that no retries will be attempted.


import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

url = 'https://example.com/api'

# Set retry values
retry_total = 5
retry_backoff_factor = 0.5
retry_status_forcelist = [500, 502, 503, 504]

# Create a new session and configure retries
session = requests.Session()
retry = Retry(total=retry_total, backoff_factor=retry_backoff_factor, status_forcelist=retry_status_forcelist)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

# Send POST request using the session
response = session.post(url)
print(response.status_code)
  

Combining Timeout and Retry Parameters

It is also possible to combine the 'timeout' and 'retry' parameters to specify a maximum time period and number of retries for a request. In this case, the request will be retried until either a response is received or the timeout value is exceeded.


import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

url = 'https://example.com/api'

# Set timeout and retry values
timeout = 5
retry_total = 5
retry_backoff_factor = 0.5
retry_status_forcelist = [500, 502, 503, 504]

# Create a new session and configure timeouts and retries
session = requests.Session()
session.timeout = timeout
retry = Retry(total=retry_total, backoff_factor=retry_backoff_factor, status_forcelist=retry_status_forcelist)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

# Send POST request using the session
try:
    response = session.post(url)
    print(response.status_code)
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
    print("Request Timed Out or Connection Error:", e)