Skip to content

Instantly share code, notes, and snippets.

@ryu22e
Last active November 5, 2021 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryu22e/b013e60df0f97542a8013ce1997ea3aa to your computer and use it in GitHub Desktop.
Save ryu22e/b013e60df0f97542a8013ce1997ea3aa to your computer and use it in GitHub Desktop.
Retry POST requests with Python Requests
#!/usr/bin/env python
from requests import Session
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util import Retry
def main():
retries = Retry(
total=5,
backoff_factor=1,
status_forcelist=[500, 502, 503, 504, 521],
allowed_methods=["POST"], # need this
)
s = Session()
s.mount("https://", HTTPAdapter(max_retries=retries))
url = "https://httpbin.org/status/500"
s.post(url) # Raise requests.exceptions.RetryError
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment