I've been addicted to it, so I made a memo for the memorial service.
When using the requests module of python, if you want to implement retry processing, you can use Retry of urllib3.util.
When status code 500 came back in post this time, I wanted to wait for a certain period of time and then retry, so I used Retry, but even if 500 is returned, it does not retry ... In conclusion, if you want to retry in post, you have to explicitly allow the POST request by passing the allowed_methos argument.
test.py
import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
import logging
logging.basicConfig(level=logging.DEBUG)
session = requests.Session()
retries = Retry(total=5,backoff_factor=1,allowed_methods=['POST'],status_forcelist = [500])
retries_default = Retry(total=5,backoff_factor=1,status_forcelist = [500])
logging.debug(repr(retries.__dict__))
logging.debug(repr(retries_default.__dict__)) #If not specified, allowed_methos has no POST
session.mount('https://',HTTPAdapter(max_retries=retries))
session.post('https://httpbin.org/status/500')
Recommended Posts