diff --git a/HDMI-Tool/src/publish_gitea.py b/HDMI-Tool/src/publish_gitea.py index 87f95c7..94ed15c 100644 --- a/HDMI-Tool/src/publish_gitea.py +++ b/HDMI-Tool/src/publish_gitea.py @@ -23,6 +23,7 @@ import re import ssl import subprocess import sys +import time import urllib.error import urllib.request import uuid @@ -59,15 +60,22 @@ class Api: self.headers = {'Authorization': f'token {token}'} self.ctx = ssl._create_unverified_context() if insecure else None - def request(self, method, path, data=None, headers=None): - req = urllib.request.Request(self.api + path, data=data, method=method, - headers={**self.headers, **(headers or {})}) - try: - with urllib.request.urlopen(req, context=self.ctx) as resp: - body = resp.read() - return resp.status, json.loads(body) if body else None - except urllib.error.HTTPError as e: - return e.code, json.loads(e.read() or b'{}') + def request(self, method, path, data=None, headers=None, retries=4): + """发请求; 实例 TLS 间歇性抽风(SSL UNEXPECTED_EOF), 连接级错误自动重试""" + last = None + for attempt in range(retries): + req = urllib.request.Request(self.api + path, data=data, method=method, + headers={**self.headers, **(headers or {})}) + try: + with urllib.request.urlopen(req, context=self.ctx) as resp: + body = resp.read() + return resp.status, json.loads(body) if body else None + except urllib.error.HTTPError as e: + return e.code, json.loads(e.read() or b'{}') + except (urllib.error.URLError, ssl.SSLError, OSError) as e: + last = e + time.sleep(1.0 + attempt) + raise RuntimeError(f'Gitea 请求连续 {retries} 次失败: {last}') def get_json(self, path): status, data = self.request('GET', path)