# HG changeset patch # User "" <;> # Date 1228213504 -3600 # Branch trunk # Node ID 35e81467ab0d1d1f560bfa89cb7ad9777a56473a # Parent 15f11caece7eb681e162b81c7fe94618d9ad386b Refactor POP3_SSL. diff --git a/Lib/poplib.py b/Lib/poplib.py --- a/Lib/poplib.py +++ b/Lib/poplib.py @@ -80,10 +80,13 @@ timeout=socket._GLOBAL_DEFAULT_TIMEOUT): self.host = host self.port = port - self.sock = socket.create_connection((host, port), timeout) + self.sock = self._create_socket(timeout) self.file = self.sock.makefile('rb') self._debugging = 0 self.welcome = self._getresp() + + def _create_socket(self,timeout): + return socket.create_connection((self.host, self.port), timeout) def _putline(self, line): @@ -349,78 +352,16 @@ See the methods of the parent class POP3 for more documentation. """ - def __init__(self, host, port = POP3_SSL_PORT, keyfile = None, certfile = None): - self.host = host - self.port = port + def __init__(self, host, port = POP3_SSL_PORT, + keyfile = None, certfile = None, + timeout = socket._GLOBAL_DEFAULT_TIMEOUT): self.keyfile = keyfile self.certfile = certfile - self.buffer = "" - msg = "getaddrinfo returns an empty list" - self.sock = None - for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM): - af, socktype, proto, canonname, sa = res - try: - self.sock = socket.socket(af, socktype, proto) - self.sock.connect(sa) - except socket.error, msg: - if self.sock: - self.sock.shutdown(socket.SHUT_RDWR) - self.sock.close() - self.sock = None - continue - break - if not self.sock: - raise socket.error, msg - self.file = self.sock.makefile('rb') - self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile) - self._debugging = 0 - self.welcome = self._getresp() + POP3.__init__(self, host, port, timeout) - def _fillBuffer(self): - localbuf = self.sslobj.read() - if len(localbuf) == 0: - raise error_proto('-ERR EOF') - self.buffer += localbuf - - def _getline(self): - line = "" - renewline = re.compile(r'.*?\n') - match = renewline.match(self.buffer) - while not match: - self._fillBuffer() - match = renewline.match(self.buffer) - line = match.group(0) - self.buffer = renewline.sub('' ,self.buffer, 1) - if self._debugging > 1: print '*get*', repr(line) - - octets = len(line) - if line[-2:] == CRLF: - return line[:-2], octets - if line[0] == CR: - return line[1:-1], octets - return line[:-1], octets - - def _putline(self, line): - if self._debugging > 1: print '*put*', repr(line) - line += CRLF - bytes = len(line) - while bytes > 0: - sent = self.sslobj.write(line) - if sent == bytes: - break # avoid copy - line = line[sent:] - bytes = bytes - sent - - def quit(self): - """Signoff: commit changes on server, unlock mailbox, close connection.""" - try: - resp = self._shortcmd('QUIT') - except error_proto, val: - resp = val - self.sock.shutdown(socket.SHUT_RDWR) - self.sock.close() - del self.sslobj, self.sock - return resp + def _create_socket(self, timeout): + sock = POP3._create_socket(self, timeout) + return ssl.wrap_socket(sock, self.keyfile, self.certfile) __all__.append("POP3_SSL")