eva icon indicating copy to clipboard operation
eva copied to clipboard

请求头需要增加默认User-Agent,不然Cloudflare会报错

Open thetbw opened this issue 6 months ago • 1 comments

错误内容如下:

错误:获取模型列表失败(HTTP 403):error code: 1010

本地测试请求头添加 User-Agent 后正常

thetbw avatar May 18 '26 08:05 thetbw

问题已确认,使用urllib.request时默认User-Agent是Python-urllib/3.12,会被CloudFare拦截。

验证命令:

python3 -c "
import urllib.request, urllib.error
try:
    req = urllib.request.Request(url='http://cloudfare.com')
    r = urllib.request.urlopen(req, timeout=10)
    print('ok')
except urllib.error.HTTPError as e:
    print(f'状态码: {e.code}')
    print(f'响应体: {e.read().decode()[:500]}')
"

输出结果:

状态码: 403
响应体: error code: 1010

在命令中加入headers={'User-Agent': 'EVA'},则正常打印ok

usepr avatar May 18 '26 11:05 usepr