(<HTTPSConnection(host='www.dmxapi.cn', port=443) at 0x703f7ad375c0>, 'Connectio...
错误信息
HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ConnectTimeoutError(<HTTPSConnection(host='www.dmxapi.cn', port=443) at 0x703f7ad375c0>, 'Connection to www.dmxapi.cn tim...
AI 解决方案
报错分析报告
1.错误翻译
原文:Max retries exceeded with url: /v1/chat/completions (Caused by ConnectTimeoutError(<HTTPSConnection(host='www.dmxapi.cn', port=443) at 0x703f7ad375c0>, 'Connection to www.dmxapi.cn tim...)
译文:访问URL /v1/chat/completions时超过最大重试次数(由ConnectTimeoutError引起:与host='www.dmxapi.cn'、port=443的HTTPS连接超时)
2.原因分析
www.dmxapi.cn:443的网络连接超时,多次重试后仍未建立有效连接。 HTTPSConnectionPool发起/v1/chat/completions接口请求时。3. 解决方案
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 1. 配置重试策略(指数退避)
retry_strategy = Retry(
total=3, # 总重试次数
backoff_factor=1, # 重试间隔:{backoff_factor} * (2 **({重试次数}-1))
status_forcelist=[429, 500, 502, 503, 504] # 需重试的状态码
)
adapter = HTTPAdapter(max_retries=retry_strategy)
# 2. 创建会话并挂载适配器
session = requests.Session()
session.mount("https://", adapter)
# 3. 发起请求(设置合理超时)
try:
response = session.post(
url="https://www.dmxapi.cn/v1/chat/completions",
timeout=(5, 30) # 连接超时5秒,读取超时30秒
)
response.raise_for_status() # 抛出HTTP错误
print("请求成功:", response.json())
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
4. 预防措施
timeout=(5,30))。 Retry配置)。 curl -v https://www.dmxapi.cn诊断网络连通性,用ping www.dmxapi.cn检查主机可达性。