Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleanly setting max_retries on Python requests get or post method

Tags:

Related to older version of requests question: Can I set max_retries for requests.request?

I have not seen an example to cleanly incorporate max_retries in a requests.get() or requests.post() call.

Would love a

requests.get(url, max_retries=num_max_retries))

implementation

like image 731
paragbaxi Avatar asked Jan 27 '14 01:01

paragbaxi


People also ask

How do I bypass max retries exceeded with URL?

To solve the requests "ConnectionError: Max retries exceeded with url", use a Retry object and specify how many connection-related errors to retry on and set a backoff factor to apply between attempts.

What is post method in Python?

The POST method is used to send data mostly through a form to the server for creating or updating data in the server. The requests module provides us with post method which can directly send the data by taking the URL and value of the data parameter.


1 Answers

A quick search of the python-requests docs will reveal exactly how to set max_retries when using a Session.

To pull the code directly from the documentation:

import requests
s = requests.Session()
a = requests.adapters.HTTPAdapter(max_retries=3)
b = requests.adapters.HTTPAdapter(max_retries=3)
s.mount('http://', a)
s.mount('https://', b)
s.get(url)

What you're looking for, however, is not configurable for several reasons:

  1. Requests no longer provides a means for configuration

  2. The number of retries is specific to the adapter being used, not to the session or the particular request.

  3. If one request needs one particular maximum number of requests, that should be sufficient for a different request.

This change was introduced in requests 1.0 over a year ago. We kept it for 2.0 purposefully because it makes the most sense. We also will not be introducing a parameter to configure the maximum number of retries or anything else, in case you were thinking of asking.


Edit Using a similar method you can achieve a much finer control over how retries work. You can read this to get a good feel for it. In short, you'll need to import the Retry class from urllib3 (see below) and tell it how to behave. We pass that on to urllib3 and you will have a better set of options to deal with retries.

from requests.packages.urllib3 import Retry
import requests

# Create a session
s = requests.Session()

# Define your retries for http and https urls
http_retries = Retry(...)
https_retries = Retry(...)

# Create adapters with the retry logic for each
http = requests.adapters.HTTPAdapter(max_retries=http_retries)
https = requests.adapters.HTTPAdapter(max_retries=https_retries)

# Replace the session's original adapters
s.mount('http://', http)
s.mount('https://', https)

# Start using the session
s.get(url)
like image 177
Ian Stapleton Cordasco Avatar answered Oct 24 '22 07:10

Ian Stapleton Cordasco