3

I need to check the timeout of a SMTP-Server, but my socket just closes. What am I doing wrong? Here is my test for it:

#!/usr/bin/python
import smtplib
import time
import datetime
import socket
socket.setdefaulttimeout(1800)


now = time.time()
server = smtplib.SMTP()
server.set_debuglevel(1)
server.connect('mx.foo.bar','25')
(code,resp) = server.docmd('NOOP')
then = time.time()

print then-now

Lets hope this works.

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
leto
  • 41
  • 1
  • 3
  • Why do you want to do this? You connect, you send your mail, you disconnect. Why do you want to keep the connection open if you're not using it? – Jerry Coffin Oct 20 '10 at 13:18
  • I'm trying to check RFC compliance. Python was suggested, instead of C for it's ease of use. – leto Oct 20 '10 at 13:35

3 Answers3

9

Well, I haven't found any method to hold a smtp connection open with smtplib.

But, if you want to reuse a connection without closing (yes, opening a connection takes time, 2-3 secs), you can test the connection first. To do this, issue a NOOP command and test for status == 250. If not, then you can open a connection and send out your mail. And you can choose to not quit() the connection until you are done.

import smtplib

def create_conn():
    conn = smtplib.SMTP('smtp.gmail.com', 587)
    ...
    return conn

def is_connected(conn):
    try:
        status = conn.noop()[0]
    except:  # smtplib.SMTPServerDisconnected
        status = -1
    return True if status == 250 else False
Ethan
  • 4,915
  • 1
  • 28
  • 36
1

Are you sure you are disconnecting? When I run the above code against a postfix server I get:

connect: ('server', '25')
connect: ('ip.address', 25)
reply: '220 server ESMTP Postfix\r\n'
reply: retcode (220); Msg: nserver ESMTP Postfix
connect: server ESMTP Postfix
send: 'NOOP\r\n'
reply: '250 2.0.0 Ok\r\n'
reply: retcode (250); Msg: 2.0.0 Ok
0.0531799793243

The docmd does not block, the server responses and the program exits. I therefore disconnect when the program exits.

If I open a python command line and do:

>> import smtplib
>> server = smtplib.SMTP()
>> server.connect('server')
>> server.docmd('NOOP')
(250, '2.0.0 Ok')
>> ## let it sit for 5 minutes
>> server.docmd('NOOP')
(421, '4.4.2 server Error: timeout exceeded')

My logs confirm this:

Oct 20 10:45:35 [postfix/smtpd] connect from unknown[ip.address]
Oct 20 10:50:10 [postfix/smtpd] timeout after NOOP from unknown[ip.address]
Mark
  • 106,305
  • 20
  • 172
  • 230
0

Check your mail server settings - they may be cutting the connection.

Andrew Sledge
  • 10,163
  • 2
  • 29
  • 30
  • It's happening with every MTA I checked. They get the command, return 220, then the connection is closed. Using telnet I get a timeout after 5 Minutes. – leto Oct 20 '10 at 13:12