You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Selenium中"upstream connect error or disconnect/reset before headers. reset reason: overflow"错误?

问题解决思路与方案

1. 先修正代码中的变量引用错误

你的代码存在明显的变量名错误:函数内定义的是proxy_ip_port,但添加代理参数时误用了self.proxy_ip_port(普通函数内不存在self上下文),这会触发NameError。修正后的代码如下:

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service

def request():
    proxy_ip_port = '129.41.171.244:8000'
    PATH = '/path/to/your/chromedriver'  # 替换为你的ChromeDriver实际路径

    options = webdriver.ChromeOptions()
    # 修正变量引用,去掉self,同时明确代理协议(默认http)
    options.add_argument(f'--proxy-server=http://{proxy_ip_port}')
    # Selenium 4.x推荐用Service替代executable_path
    driver = webdriver.Chrome(
        service=Service(PATH),
        options=options
    )

    driver.get('http://icanhazip.com')
    time.sleep(5)
    driver.quit()

2. 排查代理服务器问题

错误提示upstream connect error or disconnect/reset before headers. reset reason: overflow大多和代理本身有关,按以下步骤验证:

  • 测试代理可用性:在Ubuntu终端执行命令,直接用代理访问目标网站,确认代理是否正常工作:
    curl -x http://129.41.171.244:8000 http://icanhazip.com
    
    返回代理IP说明代理有效;若超时或报错,更换可用代理。
  • 确认代理类型:如果是SOCKS5代理,需修改Chrome参数为:
    options.add_argument(f'--proxy-server=socks5://{proxy_ip_port}')
    

3. 检查Ubuntu系统网络配置

  • 避免全局代理冲突:打开系统设置→网络→代理,设置为"手动"并填写代理信息,或保持"无"让Chrome单独使用指定代理。
  • 临时禁用防火墙:执行sudo ufw disable关闭ufw,排除安全工具拦截网络的可能。

4. 匹配Chrome与ChromeDriver版本

Selenium对Chrome和ChromeDriver版本匹配要求严格:

  • 查看Chrome版本:google-chrome --version
  • 下载对应版本的ChromeDriver,替换代码中的PATH路径
  • 给予ChromeDriver执行权限:chmod +x /path/to/your/chromedriver

内容的提问来源于stack exchange,提问作者ss_Clara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 07:45:17