peer closed connection without sending complete message body (incomplete chunked...
错误信息
peer closed connection without sending complete message body (incomplete chunked...
AI 解决方案
报错分析报告
1.错误翻译
原文:peer closed connection without sending complete message body (incomplete chunked...)
译文:对等方关闭了连接,未发送完整的消息体(不完整的分块编码...)
2. 原因分析
3.解决方案
以Python
requests 库为例,通过重试机制+超时控制修复:import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 配置重试策略(最多3次,指数退避)
retry_strategy = Retry(
total=3,
backoff_factor=1,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=["GET", "POST"] # 允许重试的请求类型
)
adapter = HTTPAdapter(max_retries=retry_strategy)
session = requests.Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
try:
# 设置超时(连接5秒,读取30秒)
response = session.get(
"https://target-url.com/api",
timeout=(5, 30),
stream=False # 非流式读取,避免分块处理异常
)
response.raise_for_status() # 检查HTTP状态码
except requests.exceptions.ChunkedEncodingError as e:
print(f"分块编码错误:{e} → 已触发重试")
except requests.exceptions.RequestException as e:
print(f"请求失败:{e}")
4. 预防措施
推荐工具:
urllib3.Retry(内置重试)、tenacity(第三方重试库); curl -v https://url 查看分块传输细节)。---
如需分析其他报错,请提供具体错误信息。