为何使用Python代码连接代理服务器时出现SSL连接错误?
解决requests代理连接时的SSLZeroReturnError错误
针对你遇到的requests.exceptions.SSLError: SSLZeroReturnError(6, 'TLS/SSL connection has been closed (EOF)')错误,可尝试以下几种解决方案:
1. 验证代理服务器可用性
先确认代理103.97.177.12:7890是否正常工作,用curl命令测试:
curl -x http://103.97.177.12:7890 https://ipinfo.io/json -k
如果curl也返回类似错误,说明代理本身不可用,需更换代理或联系提供商排查。
2. 调整代理协议配置
很多HTTPS代理实际要求客户端用HTTP协议与代理建立连接(代理再转发HTTPS请求),修改代理配置为:
import requests proxy_servers = { 'https': 'http://103.97.177.12:7890', # 将https://改为http:// } response = requests.get('https://ipinfo.io/json', proxies=proxy_servers, verify=False) print(response.json()['country']) print(response.json()['region'])
3. 添加超时与重试机制
连接不稳定可能导致EOF错误,通过Session和重试策略提升鲁棒性:
import requests from requests.adapters import HTTPAdapter from urllib3.util.retry import Retry proxy_servers = { 'https': 'http://103.97.177.12:7890', } # 配置重试策略 session = requests.Session() retry_strategy = Retry( total=3, backoff_factor=1, # 重试间隔递增 status_forcelist=[429, 500, 502, 503, 504], allowed_methods=["GET"] ) adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("https://", adapter) session.mount("http://", adapter) try: response = session.get('https://ipinfo.io/json', proxies=proxy_servers, verify=False, timeout=10) response.raise_for_status() # 主动抛出HTTP错误 data = response.json() print(data['country']) print(data['region']) except requests.exceptions.RequestException as e: print(f"请求失败: {e}")
4. 更新依赖库
旧版本的requests或urllib3可能存在SSL握手bug,升级到最新版本:
pip install --upgrade requests urllib3
内容的提问来源于stack exchange,提问作者abdelrehman99
相关产品推荐
相关产品推荐

