views:

308

answers:

1

I'm having problems with smtplib tying up my program when email sending fails, because a timeout is never raised. The server I'm using does not and will never have python greater than 2.4, so I can't make use of the timeout argument to the SMTP constructor in later versions of python.

Python 2.4's docs show that the SMTP class does not have the 'timeout' argument:

class SMTP([host[, port[, local_hostname]]])

So how do I simulate this functionality?

+4  A: 
import socket
socket.setdefaulttimeout(120)

will make any socket time out after 2 minutes, unless the specific socket's timeout is changed (and I believe SMTP in Python 2.4 doesn't do the latter).

Edit: apparently per OP's comment this breaks TLS, so, plan B...:

What about grabbing 2.6's smtplib source file and backporting it as your own module to your 2.4? Shouldn't be TOO hard... and does support timeout cleanly!-)

See here...

Alex Martelli
unfortunately this breaks starttls() functionality. my code now gets stuck at the smtp.ehlo() after smtp.starttls()
Shabbyrobe
OK, next try then -- what about grabbing 2.6's smtplib and backporting it as your own module to your 2.4? Shouldn't be TOO hard... and does support timeout cleanly!-)
Alex Martelli
Brilliant, worked a treat
Shabbyrobe