0

I am testing with a url that will return 503 and would want the script to request the URL with a backoff_factor of 1 with max _retries function. Base on my 8 times retries, theoretically the request was supposed to spend at least 64 s for the request but it just straight away print the response 503 and I do not think it actually retries for 8 times. I even increase backoff_factor to 20 and still instantly print out response 503

from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
import requests

url = 'http://httpstat.us/503'
s = requests.Session()
retries = Retry(total=8, backoff_factor=1, status_forcelist=[ 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
response = s.get(url)
print response

I have been checking other post and try with s.mount('http://', requests.adapters.HTTPAdapter(max_retries=retries)) with similar result. How to let the script really retries 8 times and confirm it actually retries? Please advise if the solution works for HTTP and https

When I call retries

>>> retries
Retry(total=8, connect=None, read=None, redirect=None)

Is the input correct? I base on https://stackoverflow.com/a/35636367/5730859 solution and but it does not seems to retry. Can anyone help?

bkcollection
  • 913
  • 1
  • 12
  • 35
  • 1) the server will have a much faster response time when it errors or there is a client-side deformity. 2) Assuming the server is setup correctly, 503 means the service/endpoint is unavailable. – Noah Mar 26 '21 at 04:44

1 Answers1

-1

Updating to newest comments. Instead of using that urllib3 function, use your own:

>>> def test():
    return requests.get("http://httpstat.us/503").status_code

>>> def main():
    total_retries = 0
    code = test()
    while code == 503 or 502 or 504:
        total_retries+=1
        if total_retries > 8:
            print("8 retries hit")
            break
        code = test()

        
>>> main()
8 retries hit
>>> 

This will stop once it hits 8 retries with the 503, 504, 502 code(s).

Noah
  • 194
  • 1
  • 8
  • Nope. I am using 503 as an example. The URL just an example. You can use any URL that return 503. I mean the script does not retries even it is error 503, what is the problem? – bkcollection Mar 26 '21 at 05:17
  • If it is just an "example" problem then you must show the URL you are requesting. That is a server-side error, so it is not the client but the server that is "messing up". Read what i said, what you are doing really makes no sense. – Noah Mar 26 '21 at 05:52
  • there is no retries for this confirm 503 with this URL `http://httpstat.us/503` – bkcollection Mar 26 '21 at 05:59
  • take a look at the new answer – Noah Mar 26 '21 at 06:22
  • Could your solution base on my snippet? The solution you provided I have tried previously but I need something more pythonic – bkcollection Mar 26 '21 at 06:53
  • when you run my code, do you get any retries? retries suppose to take times instead to get 503 instantly – bkcollection Mar 26 '21 at 07:15
  • you are not making any sense at all. You are importing the requests library to use another library's functions. I gave you a working solution for ```requests```. – Noah Mar 26 '21 at 14:42
  • your solution is not the elegant way. – bkcollection Mar 27 '21 at 09:46