Last active
November 5, 2021 02:27
-
-
Save ryu22e/b013e60df0f97542a8013ce1997ea3aa to your computer and use it in GitHub Desktop.
Retry POST requests with Python Requests
This file contains hidden or 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
#!/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