修复 requests 2.34+ 移除 ProxyError 导致的登录报错
- requests 2.34+ 已移除 ProxyError 属性 - 代理错误统一为 ConnectionError,通过错误信息判断是否为代理问题 - 代理错误时刷新代理并重试,非代理连接错误也重试 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
1b20317b42
commit
bc40d334b9
+16
-5
@@ -162,16 +162,27 @@ class DouyuLogin:
|
||||
f"{method.upper()} {safe_url} 超时,耗时 {elapsed:.1f}s,"
|
||||
f"timeout={timeout}"
|
||||
) from exc
|
||||
except requests.ProxyError as exc:
|
||||
except requests.ConnectionError as exc:
|
||||
# requests 2.34+ 已移除 ProxyError,代理错误统一为 ConnectionError
|
||||
is_proxy_err = "proxy" in str(exc).lower() or "Proxy" in type(exc).__name__
|
||||
if is_proxy_err:
|
||||
elapsed = time.monotonic() - started
|
||||
logger.warning(f"代理连接失败,尝试 {attempt + 1}/{max_retries}: {exc}")
|
||||
if attempt < max_retries - 1:
|
||||
self._refresh_proxy()
|
||||
time.sleep(1)
|
||||
continue
|
||||
raise ConnectionError(
|
||||
f"{method.upper()} {safe_url} 代理连接失败,已重试 {max_retries} 次"
|
||||
) from exc
|
||||
# 非代理的 ConnectionError 也重试
|
||||
elapsed = time.monotonic() - started
|
||||
logger.warning(f"代理连接失败,尝试 {attempt + 1}/{max_retries}: {exc}")
|
||||
logger.warning(f"连接失败,尝试 {attempt + 1}/{max_retries}: {exc}")
|
||||
if attempt < max_retries - 1:
|
||||
# 刷新代理
|
||||
self._refresh_proxy()
|
||||
time.sleep(1)
|
||||
continue
|
||||
raise ConnectionError(
|
||||
f"{method.upper()} {safe_url} 代理连接失败,已重试 {max_retries} 次"
|
||||
f"{method.upper()} {safe_url} 连接失败,已重试 {max_retries} 次"
|
||||
) from exc
|
||||
except requests.RequestException as exc:
|
||||
elapsed = time.monotonic() - started
|
||||
|
||||
Reference in New Issue
Block a user