from requests.adapters import HTTPAdapter, Retry from requests.auth import HTTPDigestAuth, HTTPBasicAuth from requests.exceptions import ConnectionError, RetryError import time, requests with open('./snapshot.jpg', 'wb') as handle: with requests.Session() as s: retries = Retry(total = 3, backoff_factor=0.1, status_forcelist=[404,408, 500, 502, 503, 504]) s.mount('http://', HTTPAdapter(max_retries=retries)) resp = "" try: resp = s.get("http://192.168.1.97/cgi-bin/jpg/image.cgi?id=2.1212121212", auth=HTTPDigestAuth("admin", "Amerus@1903"), stream=True) except RetryError as m: print(m) if resp: for block in resp.iter_content(1024): if not block: break handle.write(block)