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

使用SOCKS5代理连接Gmail SMTP服务器时出现连接意外关闭问题求助

使用SOCKS5代理连接Gmail SMTP服务器时出现连接意外关闭问题求助

我写了一段简单的代码来验证SOCKS5代理是否能正常连接Gmail的SMTP服务器,但一直遇到连接意外关闭的错误,想请大家帮忙看看问题出在哪。

我的代理验证代码

import smtplib, socks

socks.set_default_proxy(socks.PROXY_TYPE_SOCKS5, '*.*.*.*', <PORT>, username='<UNAME>', password='<PWD>')
socks.wrapmodule(smtplib)

server = smtplib.SMTP('gmail-smtp-in.l.google.com', 25)
server.set_debuglevel(1) # 开启SMTP对话日志
server.ehlo()
server.quit()

报错信息

运行后直接抛出了连接断开的异常,完整Traceback如下:

Traceback (most recent call last):
  File "/Users/***/***/***/test.py", line 10, in <module>
    server = smtplib.SMTP('gmail-smtp-in.l.google.com', 25)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
    ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/smtplib.py", line 405, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

我已经做的排查步骤

  • Ping目标地址:我ping了gmail-smtp-in.l.google.com,网络连通性正常,没有丢包问题。
  • 测试代理基础连通性:我写了一段代码测试代理能否连接到目标服务器,只做connect操作时显示代理可用:
def test_proxy_connectivity(proxy, port):
    ip, port_, user, pwd = proxy
    try:
        s = socks.socksocket()
        s.set_proxy(socks.SOCKS5, ip, port_, username=user, password=pwd)
        s.settimeout(5)
        s.connect(('gmail-smtp-in.l.google.com', port))
        s.close()
        print(f'proxy available {ip}:{port_}')
        return True
    except Exception as e:
        print(f'proxy UNavailable: {ip}:{port_} - {e}')
        return False

test_proxy_connectivity(PROXY.values(),25)

输出结果:proxy availible <SAMEPROXYHERE>

  • 测试带数据接收的连通性:但当我在测试代码里加上s.recv(1024)尝试接收服务器响应时,直接超时了:
def test_proxy_connectivity(proxy, port):
    ip, port_, user, pwd = proxy
    try:
        s = socks.socksocket()
        s.set_proxy(socks.SOCKS5, ip, port_, username=user, password=pwd)
        s.settimeout(5)
        s.connect(('gmail-smtp-in.l.google.com', port))
        s.recv(1024).decode(errors='ignore') # 新增接收响应的代码
        s.close()
        print(f'proxy available {ip}:{port_}')
        return True
    except Exception as e:
        print(f'proxy Unavailable: {ip}:{port_} - {e}')
        return False

test_proxy_connectivity(PROXY.values(),25)

输出结果:proxy UNavailible: <SAMEPROXYHERE> - timed out

另外我查过这个代理IP,它不在Spamhaus这类常见的黑名单里,实在搞不懂为什么连接SMTP服务器会失败,有没有大佬能指点一下?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:24:38