, HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with...
🚨 错误信息
Run failed: [models] Server Unavailable Error, HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x7fdcaaedc0b0>, 'Connection to www.dmxapi.cn timed out. (connect timeout=10)'))
🤖 AI解决方案
错误翻译
原文:HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ConnectTimeoutError(, 'Connection to www.dmxapi.cn timed out. (connect timeout=10)'))
译文:HTTPS连接池(主机='www.dmxapi.cn', 端口=443):URL /v1/chat/completions 超出最大重试次数(原因为连接超时错误:连接到 www.dmxapi.cn 超时(连接超时时间=10秒))
原因分析
根本原因:网络连接超时,无法连接到DMXAPI服务器的聊天补全接口。
具体分析:
www.dmxapi.cn:443 的 /v1/chat/completions 端点解决方案
立即检查步骤:
python
import requests
import socket1. 检查网络连通性
try:
response = requests.get('https://www.dmxapi.cn', timeout=10)
print(f"服务器状态: {response.status_code}")
except Exception as e:
print(f"连接失败: {e}")2. 检查DNS解析
try:
ip = socket.gethostbyname('www.dmxapi.cn')
print(f"域名解析IP: {ip}")
except Exception as e:
print(f"DNS解析失败: {e}")
代码修复方案:
python
增加超时时间和重试机制
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry配置重试策略
retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
)创建会话并配置适配器
session = requests.Session()
adapter = HTTPAdapter(max_retries=retry_strategy)
session.mount("http://", adapter)
session.mount("https://", adapter)使用会话发送请求(增加超时时间)
try:
response = session.post(
'https://www.dmxapi.cn/v1/chat/completions',
timeout=30, # 增加超时时间
json=your_payload
)
response.raise_for_status()
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
️ 预防措施
1. 连接可靠性优化
2. 监控和备用方案
python
添加健康检查和备用端点
def check_server_health():
endpoints = [
'https://www.dmxapi.cn/v1/health',
'https://backup.dmxapi.cn/v1/health'
]
for endpoint in endpoints:
try:
response = requests.get(endpoint, timeout=5)
if response.status_code == 200:
return endpoint.replace('/health', '')
except:
continue
return None
推荐工具:使用 requests 的 Session 对象管理连接池,考虑使用 tenacity 库实现更复杂的重试逻辑。