From 597092de15309c2cc4aa2b89561fd156c1ec254a Mon Sep 17 00:00:00 2001 From: Andreas Sauerwein-Schlosser Date: Thu, 20 Jun 2024 19:54:38 +0200 Subject: [PATCH] try 3 times on imcomplete read --- download_v2.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/download_v2.py b/download_v2.py index ee6c940..5effd20 100755 --- a/download_v2.py +++ b/download_v2.py @@ -23,6 +23,7 @@ import smtplib from email.message import EmailMessage import logging +from time import sleep # setup logger logger = logging.getLogger(__name__) @@ -96,7 +97,16 @@ def base_req(url: str, method="get", json={}): # "x-tesla-user-agent": "TeslaApp/4.28.3-2167", } logging.info(f"{method} Request to url: {url}") - result = sess.request(method=method, url=url, headers=headers, json=json) + for attempt in range(3): + try: + result = sess.request(method=method, url=url, headers=headers, json=json) + break + except requests.exceptions.ChunkedEncodingError: + logger.warning(f"incomplete read occured, attempt {attempt} of 3") + sleep(1) + else: + logger.error(f"giving up after 3 tries") + exit(1) result.raise_for_status() if "application/json" in result.headers.get("Content-Type"): return result.json()