Last active
July 12, 2023 01:16
-
-
Save benjiao/28dc36bd87121b3273e0b3e079a8e8d8 to your computer and use it in GitHub Desktop.
Python requests with retry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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