From b0060eee35d5719f54e365db735e35baf4178e80 Mon Sep 17 00:00:00 2001 From: zsbai Date: Thu, 12 Feb 2026 21:43:26 +0000 Subject: [PATCH] feat: Retry added for network request; --- scripts/destVersionForMac.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/destVersionForMac.py b/scripts/destVersionForMac.py index d67770e..0275df7 100644 --- a/scripts/destVersionForMac.py +++ b/scripts/destVersionForMac.py @@ -92,11 +92,22 @@ def fetch_head_metadata(url: str) -> dict[str, str]: Returns: dict[str, str]: 从 HEAD 响应中提取的元数据字典,键为小写字符串,值为对应的响应头值 """ - # Use HEAD request to read metadata from the direct file link. - request = urllib.request.Request(url, method="HEAD") - with urllib.request.urlopen(request, timeout=30) as response: - return {key.lower(): value.strip() for key, value in response.headers.items()} + attempts = 2 + last_error: Exception | None = None + for attempt in range(1, attempts + 1): + try: + request = urllib.request.Request(url, method="HEAD") + with urllib.request.urlopen(request, timeout=30) as response: + return {key.lower(): value.strip() for key, value in response.headers.items()} + except Exception as exc: + last_error = exc + if attempt < attempts: + log(f"HEAD request failed (attempt {attempt}). Waiting before retry...") + time.sleep(10) + if last_error: + log(f"HEAD request failed after {attempts} attempts: {last_error}") + return {} def download_with_retry(url: str, dest: Path) -> None: """