Public
Authored by Bruno Duyé

Use of requests session and Retry

How to make an error resistant get or post, with many retries.

Usage: once requests_session object is initialized:

# POST
response = requests_session.post(url, json={...})
# GET
response = requests_session.get(url)
Edited
python 692 Bytes
  • Necessary imports:

    from requests import Session
    from requests.adapters import HTTPAdapter
    from urllib3.util import Retry
  • I put the whole code for convenience:

    from requests import Session
    from requests.adapters import HTTPAdapter
    from urllib3.util import Retry
    
    requests_retry = Retry(total=12, backoff_factor=2, status_forcelist=[
                           429, 500, 501, 502, 503, 504], method_whitelist=False)
    requests_session = Session()
    requests_session.mount('http://', HTTPAdapter(max_retries=requests_retry))
    requests_session.mount('https://', HTTPAdapter(max_retries=requests_retry))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment