I believe that I've seen this discussed previously, so maybe there's
some interest in it. I wrote a threaded mail filtering framework a
while ago, and one of the modules does address verification via SMTP.
Since smtplib.SMTP uses blocking IO, it can block the whole
interpreter. Sometimes the whole thing would stop working indefinitely.
I'm now aware that Twisted offers a non-blocking SMTP class, but I
didn't really want to make that a dependency of the address
verification. I ended up subclassing the smtplib.SMTP class and
rewriting the functions that do I/O. Perhaps someone who doesn't want
to install Twisted will find this class useful, someday. It doesn't
support TLS, but it is otherwise a thread-safe SMTP class.
class ThreadSMTP(smtplib.SMTP):
"""SMTP class safe for use in threaded applications.
This class reimplements the SMTP class with non-blocking IO,
so that threaded applications don't lock up.
This class won't make starttls support thread-safe.
"""
def connect(self, host='localhost', port=0):
"""Connect to a host on a given port.
If the hostname ends with a colon (`:') followed by a number, and
there is no port specified, that suffix will be stripped off and the
number interpreted as the port number to use.
Note: This method is automatically invoked by __init__, if a host is
specified during instantiation.
"""
if not port and (host.find(':') == host.rfind(':')):
i = host.rfind(':')
if i >= 0:
host, port = host[:i], host[i+1:]
try: port = int(port)
except ValueError:
raise socket.error, "nonnumeric port"
if not port: port = smtplib.SMTP_PORT
if self.debuglevel > 0: print>>sys.stderr, 'connect:', (host, port)
msg = "getaddrinfo returns an empty list"
self.sock = None
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
self.sock = socket.socket(af, socktype, proto)
self.sock.setblocking(0)
if self.debuglevel > 0: print>>sys.stderr, 'connect:', (host, port)
# Try to connect to the non-blocking socket. We expect connect()
# to throw an error, indicating that the connection is in progress.
# Use select to wait for the connection to complete, and then check
# for errors with getsockopt.
try:
self.sock.connect(sa)
except socket.error:
readySocks = select.select([self.sock], [], [], _smtpTimeout)
if self.sock in readySocks[0]:
soError = self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if soError:
raise socket.error, 'connection failed, error: %d' % soError
else:
# The connection timed out.
raise socket.error, 'connection timed out'
except socket.error, msg:
if self.debuglevel > 0: print>>sys.stderr, 'connect fail:', (host, port)
if self.sock:
self.sock.close()
self.sock = None
continue
break
if not self.sock:
raise socket.error, msg
(code, msg) = self.getreply()
if self.debuglevel > 0: print>>sys.stderr, "connect:", msg
return (code, msg)
def send(self, str):
"""Send `str' to the server."""
if self.debuglevel > 0: print>>sys.stderr, 'send:', repr(str)
if self.sock:
try:
# Loop: Wait for select() to indicate that the socket is ready
# for data, and call send(). If send returns a value smaller
# than the total length of str, save the remaining data, and
# continue to attempt to send it. If select() times out, raise
# an exception and let the handler close the connection.
while str:
readySocks = select.select([], [self.sock], [], _smtpTimeout)
if not readySocks[1]:
raise socket.error, 'Write timed out.'
sent = self.sock.send(str)
if sent < len(str):
str = str[sent:]
else:
# All the data was written, break the loop.
break
except socket.error:
self.close()
raise smtplib.SMTPServerDisconnected('Server not connected')
else:
raise smtplib.SMTPServerDisconnected('please run connect() first')
def getreply(self):
"""Get a reply from the server.
Returns a tuple consisting of:
- server response code (e.g. '250', or such, if all goes well)
Note: returns -1 if it can't read response code.
- server response string corresponding to response code (multiline
responses are converted to a single, multiline string).
Raises SMTPServerDisconnected if end-of-file is reached.
"""
resp=[]
while 1:
try:
line = self._nonblockReadline()
except socket.error:
self.close()
raise smtplib.SMTPServerDisconnected("Connection unexpectedly closed")
if self.debuglevel > 0: print>>sys.stderr, 'reply:', repr(line)
resp.append(line[4:].strip())
code=line[:3]
# Check that the error code is syntactically correct.
# Don't attempt to read a continuation line if it is broken.
try:
errcode = int(code)
except ValueError:
errcode = -1
break
# Check if multiline response.
if line[3:4]!="-":
break
errmsg = "\n".join(resp)
if self.debuglevel > 0:
print>>sys.stderr, 'reply: retcode (%s); Msg: %s' % (errcode,errmsg)
return errcode, errmsg
def _nonblockReadline(self):
# This implementation is good enough for SMTP, but not for general-case
# use.
# Read until \n or EOF, whichever comes first
data = ""
buffers = []
recv = self.sock.recv
while data != "\n":
readySocks = select.select([self.sock], [], [], _smtpTimeout)
if not readySocks[0]:
raise socket.error, 'readline timed out'
data = recv(1)
if not data:
raise socket.error, 'connection closed'
buffers.append(data)
return string.join(buffers,"")
--
http://mail.python.org/mailman/listinfo/python-list