How to pass a retry strategy to a session in python requests

Question:

I’m using the requests package to fetch data from a web page. I created a retry strategy after a tutorial, but I don’t understand how to give it to the session.

retry_strategy = Retry(total=10,
                       backoff_factor=5,
                       status_forcelist=[429, 500, 501, 502, 503],
                       allowed_methods=["GET"])
adapter = HTTPAdapter(max_retries=retry_strategy)

If I try to pass it I get:

session = requests.Session(adapter)
TypeError: Session.__init__() takes 1 positional argument but 2 were given
Asked By: Sarah

||

Answers:

You’ve done everything right, but instead of passing the adapter you need to use the .mount() method like this:

session = requests.Session()
session.mount("https://", adapter)
Answered By: bitflip
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.