Skip to content

Instantly share code, notes, and snippets.

@benjiao
Last active July 12, 2023 01:16
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save benjiao/28dc36bd87121b3273e0b3e079a8e8d8 to your computer and use it in GitHub Desktop.
Save benjiao/28dc36bd87121b3273e0b3e079a8e8d8 to your computer and use it in GitHub Desktop.
Python requests with retry
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
with requests.Session() as s:
retries = Retry(
total=10,
backoff_factor=0.2,
status_forcelist=[500, 502, 503, 504])
s.mount('http://', HTTPAdapter(max_retries=retries))
s.mount('https://', HTTPAdapter(max_retries=retries))
r = s.post("http://google.com/")
@keith
Copy link

keith commented May 9, 2018

This doesn't actually retry on posts by default. To do that you need to pass POST in this way: https://stackoverflow.com/questions/35704392/how-to-make-python-post-requests-to-retry

@hassanseoul123
Copy link

How to get log of every retry?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment