Defines a "timeout_socket" class for connections that can potentially
cause the server to hang. It uses select, and is based on code originally
from Scott Cotton.
The following people have contributed to the development of this module:
Scott Cotton <scott@chronis.pobox.com>
Lloyd Zusman <ljz@asfast.com
Phil Mayes <pmayes@olivebr.com>
Piers Lauder <piers@cs.su.oz.au>
Here are examples using smtplib.py and poplib.py by inheriting:
class timeout_POP3(poplib.POP3):
def __init__(self, host, port = poplib.POP3_PORT):
self.host = host
self.port = port
self.sock = timeout_socket.timeout_socket()
self.sock.connect(self.host, self.port)
self.file = self.sock.makefile('rb') # same as poplib.POP3
self._debugging = 0
self.welcome = self._getresp()
class timeout_SMTP(smtplib.SMTP):
def connect(self, host='localhost', port = 0):
'''Connect to a host on a given port.
Override the std SMTP connect in order to use a timeout_socket.
'''
if not port:
i = string.find(host, ':')
if i >= 0:
host, port = host[:i], host[i+1:]
try: port = string.atoi(port)
except string.atoi_error:
raise socket.error, "nonnumeric port"
if not port: port = smtplib.SMTP_PORT
self.sock = timeout_socket.timeout_socket()
if self.debuglevel > 0: print 'connect:', (host, port)
self.sock.connect(host, port)
(code,msg)=self.getreply()
if self.debuglevel >0 : print "connect:", msg
return (code,msg)
This allows one to use poplib.py & smtplib.py unchanged.
|
Classes:
|
timeout_socket(timeout=20, sock=None) where `timeout' is the desired timeout in seconds (default 20), and `sock' is a pre-existing socket (default: one is created).