Home > front end >  How to pass a retry strategy to a session in python requests
How to pass a retry strategy to a session in python requests

Time:10-18

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

Thank you in advance!

CodePudding user response:

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)
  • Related