# HG changeset patch # User "Lorenzo M. Catucci" # Date 1228223984 -3600 # Branch trunk # Node ID 2983b7526464c6ea8b45888aee5839d55bf9c4ec # Parent 16f52cec9334b5fe830124224752a6caa2fb9452 IMAP4_SSL refactor. diff --git a/Lib/imaplib.py b/Lib/imaplib.py --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -148,7 +148,10 @@ mustquote = re.compile(r"[^\w!#$%&'*+,.:;<=>?^`|~-]") - def __init__(self, host = '', port = IMAP4_PORT): + PORT = IMAP4_PORT + + def __init__(self, host = '', port = PORT, + timeout=socket._GLOBAL_DEFAULT_TIMEOUT): self.debug = Debug self.state = 'LOGOUT' self.literal = None # A literal argument to a command @@ -160,7 +163,7 @@ # Open socket to server. - self.open(host, port) + self.open(host, port, timeout) # Create unique tag for this session, # and compile tagged response matcher. @@ -218,7 +221,8 @@ # Overridable methods - def open(self, host = '', port = IMAP4_PORT): + def open(self, host = '', port = PORT, + timeout = socket._GLOBAL_DEFAULT_TIMEOUT): """Setup connection to remote server on "host:port" (default: localhost:standard IMAP4 port). This connection will be used by the routines: @@ -226,9 +230,11 @@ """ self.host = host self.port = port - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.connect((host, port)) + self.sock = self._create_socket(timeout) self.file = self.sock.makefile('rb') + + def _create_socket(self,timeout): + return socket.create_connection((self.host, self.port), timeout) def read(self, size): @@ -1130,62 +1136,26 @@ for more documentation see the docstring of the parent class IMAP4. """ + PORT = IMAP4_SSL_PORT - def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None): + def __init__(self, host = '', port = PORT, + timeout = socket._GLOBAL_DEFAULT_TIMEOUT, + keyfile = None, certfile = None): self.keyfile = keyfile self.certfile = certfile - IMAP4.__init__(self, host, port) + IMAP4.__init__(self, host, port, timeout) - def open(self, host = '', port = IMAP4_SSL_PORT): - """Setup connection to remote server on "host:port". - (default: localhost:standard IMAP4 SSL port). - This connection will be used by the routines: - read, readline, send, shutdown. + def open(self, host = '', port = PORT, + timeout = socket._GLOBAL_DEFAULT_TIMEOUT): + """Setup connection to remote server on host:port """ - self.host = host - self.port = port - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.connect((host, port)) - self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile) + IMAP4.open(self,host,port,timeout) - def read(self, size): - """Read 'size' bytes from remote.""" - # sslobj.read() sometimes returns < size bytes - chunks = [] - read = 0 - while read < size: - data = self.sslobj.read(min(size-read, 16384)) - read += len(data) - chunks.append(data) - - return ''.join(chunks) - - - def readline(self): - """Read line from remote.""" - line = [] - while 1: - char = self.sslobj.read(1) - line.append(char) - if char == "\n": return ''.join(line) - - - def send(self, data): - """Send data to remote.""" - bytes = len(data) - while bytes > 0: - sent = self.sslobj.write(data) - if sent == bytes: - break # avoid copy - data = data[sent:] - bytes = bytes - sent - - - def shutdown(self): - """Close I/O established in "open".""" - self.sock.close() + def _create_socket(self,timeout): + sock = IMAP4._create_socket(self,timeout) + return ssl.wrap_socket(sock, self.keyfile, self.certfile) def socket(self): @@ -1201,7 +1171,7 @@ ssl = ssl.wrap_socket(.socket) """ - return self.sslobj + return self.sock __all__.append("IMAP4_SSL")