Question

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?

Was it helpful?

Solution

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top