python - With urllib3 Retry add solution for retries based on response content

Viewed 352

I am using the usual urllib3 solution for requests to have retry and timeout handling:

DEFAULT_TIMEOUT = 20 # seconds

class TimeoutHTTPAdapter(HTTPAdapter):
    def __init__(self, *args, **kwargs):
        self.timeout = DEFAULT_TIMEOUT
        if "timeout" in kwargs:
            self.timeout = kwargs["timeout"]
            del kwargs["timeout"]
        super().__init__(*args, **kwargs)

    def send(self, request, **kwargs):
        timeout = kwargs.get("timeout")
        if timeout is None:
            kwargs["timeout"] = self.timeout
        return super().send(request, **kwargs)

retry_strategy = Retry(
    total=5,
    backoff_factor=2,
    status_forcelist=[400, 408, 429, 502, 503, 504, 508],
    method_whitelist=["HEAD", "GET", "OPTIONS", "POST"]
)
adapter = TimeoutHTTPAdapter(max_retries=retry_strategy)
http = requests.Session()
http.mount("https://", adapter)
http.mount("http://", adapter)

But sometimes I receive post request responses with response code 200 who contain error messages and service timeouts.

Is it possible to handle such content based retries with the build in retry solution of urllib3 or with other solutions?

0 Answers
Related