[Python] Cannot communicate to SMTP server with socket and proxy due to timeout error

Discussão em 'Python' iniciado por Stack, Julho 8, 2025.

  1. Stack

    Stack Membro Participativo

    Here's my code for simple proxy validation:

    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 dialogue log
    server.ehlo()
    server.quit()


    Here's the output:

    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


    I've pinged gmail-smtp-in.l.google.com – it's fine. I've also tried to check if we can establish connection with server through this proxy, and it seems like we can:

    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 availible {ip}:{port_}')
    return True
    except Exception as e:
    print(f'proxy UNavailible: {ip}:{port_} - {e}')
    return False

    test_proxy_connectivity(PROXY.values(),25)


    Output:

    proxy availible <SAMEPROXYHERE>


    So it kinda makes me believe that port 25 is open on this proxy. And if we add recv there:

    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 availible {ip}:{port_}')
    return True
    except Exception as e:
    print(f'proxy UNavailible: {ip}:{port_} - {e}')
    return False

    test_proxy_connectivity(PROXY.values(),25)


    Output:

    proxy UNavailible: <SAMEPROXYHERE> - timed out


    The same problem I guess... And as far as I know this IP isn't in any blacklist like Spamhaus.

    Continue reading...

Compartilhe esta Página