I want to fetch content of a URL and in cases I face 50X HTTP error codes, retry for 10 times with 0.5 seconds delay between retries. I also want to have a 1 second timeout for my requests. To aim this goal I tried the following program:
#!/usr/bin/python3
import sys
import requests
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
URL = "http://<DOMAIN>/Loader.aspx?ParTree=15131H&i=" # + stockid
def get_data(stockid):
print("1")
req_session = requests.Session()
print("2")
retries = Retry(total = 10,
backoff_factor = 0.5,
status_forcelist = [500, 502, 503, 504])
print("3")
req_session.mount("http://", HTTPAdapter(max_retries=retries))
print("4")
page_content = req_session.get(URL + stockid, timeout=1).content
print("5")
get_data(sys.argv[1])
Unfortunately it freezes for minutes after printing 4 on the screen. What is wrong?