('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConn...

2026年01月10日 11:24 processing

错误信息

请求异常: HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000028149D576B0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

AI 解决方案

代码糕手 - 报错分析报告

1.翻译


原文:HTTPSConnectionPool(host='www.dmxapi.cn', port=443): Max retries exceeded with url: /v1/chat/completions (Caused by ProxyError('Unable to connect to proxy', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000028149D576B0>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))
译文:HTTPS连接池(主机='www.dmxapi.cn', 端口=443): 超过最大重试次数,URL: /v1/chat/completions (由代理错误引起('无法连接到代理', 新连接错误('<urllib3.connection.HTTPSConnection对象>: 建立新连接失败: [Windows错误10061] 由于目标计算机积极拒绝,无法连接。')))

2.分析


  • 根本原因:Python程序尝试通过代理服务器连接到 www.dmxapi.cn:443,但代理服务器拒绝了连接请求

  • 具体问题:代码配置了代理设置(可能是环境变量、代码中硬编码或系统设置),但代理服务器无法访问或无响应

  • 错误位置:发生在Python的urllib3库建立HTTPS连接时,具体在代理连接阶段
  • 3.

    选项1:移除代理配置(如果不需要代理)


    import requests
    import os
    
    # 清除可能影响连接的代理环境变量
    os.environ.pop('HTTP_PROXY', None)
    os.environ.pop('HTTPS_PROXY', None)
    os.environ.pop('http_proxy', None)
    os.environ.pop('https_proxy', None)
    
    # 在requests会话中明确禁用代理
    session = requests.Session()
    session.trust_env = False  # 忽略系统代理设置
    
    # 然后使用这个session发起请求
    response = session.post('https://www.dmxapi.cn/v1/chat/completions', 
                           json=your_data, 
                           headers=your_headers)
    

    选项2:检查并配置正确的代理


    import requests
    
    # 配置正确的代理(替换为你的实际代理地址)
    proxies = {
        'http': 'http://your-proxy-address:port',
        'https': 'http://your-proxy-address:port',  # 注意:某些代理也使用http
        # 或者 'https': 'https://your-proxy-address:port'
    }
    
    # 测试代理是否可用
    try:
        test_response = requests.get('https://www.baidu.com', proxies=proxies, timeout=5)
        print("代理连接成功")
    except:
        print("代理不可用,请检查代理配置")
    
    # 使用配置好的代理进行请求
    response = requests.post('https://www.dmxapi.cn/v1/chat/completions',
                            proxies=proxies,
                            json=your_data,
                            headers=your_headers)
    

    选项3:检查系统代理设置(Windows)


    # 检查当前网络代理设置
    netsh winhttp show proxy
    
    # 如果需要重置代理设置
    netsh winhttp reset proxy
    

    4.预防措施

    ###实践

  • 代理配置管理

  • - 使用配置文件或环境变量管理代理设置,避免硬编码
    - 实现代理检测机制,自动切换直连/代理模式

    • 连接异常处理

    •    import requests
         from requests.exceptions import ProxyError, ConnectionError
         
         def safe_request(url, data, headers, retries=3):
             for attempt in range(retries):
                 try:
                     response = requests.post(url, json=data, headers=headers, timeout=10)
                     return response
                 except ProxyError:
                     if attempt < retries - 1:
                         # 尝试禁用代理重试
                         response = requests.post(url, json=data, headers=headers, 
                                                timeout=10, proxies={})
                         return response
                 except ConnectionError as e:
                     print(f"连接失败,重试 {attempt + 1}/{retries}: {e}")
                     continue
             return None
         

      推荐工具

    • 代理调试工具:Fiddler、Charles Proxy 用于检查代理配置

    • 网络诊断:使用 curl 测试连接:curl -v https://www.dmxapi.cn

    • Python库:使用 requestsSession 对象管理连接和重试策略

    总结:这是一个网络代理配置问题,程序尝试通过无法访问的代理服务器连接到目标API。建议先检查代理设置,如果不需要代理,请明确禁用;如果需要代理,请确保代理服务器正常运行且配置正确。