[Python-checkins] python/dist/src/Lib poplib.py,1.21,1.22
loewis at users.sourceforge.net
loewis at users.sourceforge.net
Fri Oct 31 07:52:37 EST 2003
Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv5427/Lib
Modified Files:
poplib.py
Log Message:
Patch #: Add POP3 over SSL support.
Index: poplib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/poplib.py,v
retrieving revision 1.21
retrieving revision 1.22
diff -C2 -d -r1.21 -r1.22
*** poplib.py 1 Jun 2002 16:07:16 -0000 1.21
--- poplib.py 31 Oct 2003 12:52:35 -0000 1.22
***************
*** 8,11 ****
--- 8,12 ----
# Updated: Piers Lauder <piers at cs.su.oz.au> [Jul '97]
# String method conversion and test jig improvements by ESR, February 2001.
+ # Added the POP3_SSL class. Methods loosely based on IMAP_SSL. Hector Urtubia <urtubia at mrbook.org> Aug 2003
# Example (see the test function at the end of this file)
***************
*** 15,19 ****
import re, socket
! __all__ = ["POP3","error_proto"]
# Exception raised when an error or invalid response is received:
--- 16,20 ----
import re, socket
! __all__ = ["POP3","error_proto","POP3_SSL"]
# Exception raised when an error or invalid response is received:
***************
*** 24,27 ****
--- 25,31 ----
POP3_PORT = 110
+ # POP SSL PORT
+ POP3_SSL_PORT = 995
+
# Line terminators (we always output CRLF, but accept any of CRLF, LFCR, LF)
CR = '\r'
***************
*** 317,320 ****
--- 321,408 ----
return self._shortcmd('UIDL %s' % which)
return self._longcmd('UIDL')
+
+ class POP3_SSL(POP3):
+ """POP3 client class over SSL connection
+
+ Instantiate with: POP3_SSL(hostname, port=995, keyfile=None, certfile=None)
+
+ hostname - the hostname of the pop3 over ssl server
+ port - port number
+ keyfile - PEM formatted file that countains your private key
+ certfile - PEM formatted certificate chain file
+
+ 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
+ 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.close()
+ self.sock = None
+ continue
+ break
+ if not self.sock:
+ raise socket.error, msg
+ self.file = self.sock.makefile('rb')
+ self.sslobj = socket.ssl(self.sock, self.keyfile, self.certfile)
+ self._debugging = 0
+ self.welcome = self._getresp()
+
+ 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*', `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*', `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.close()
+ del self.sslobj, self.sock
+ return resp
More information about the Python-checkins
mailing list