[Python-Dev] imaplib.py and SSL

Tino Lange tino.lange@isg.de
Fri, 01 Mar 2002 14:31:53 +0100


This is a multi-part message in MIME format.
--------------096780CBE735D0919E9873DF
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hallo!

Our company has decided to allow only SSL connections to the e-mailbox
from outside. So I needed a SSL capable "imaplib.py" to run my
mailwatcher-scripts from home.

Thanks to the socket.ssl() in recent Pythons it was nearly no problem to
derive an IMAP4_SSL-class from the existing IMAP4-class in Python's
standard library.

Maybe you want to look over the very small additions that were necessary
to implement the IMAP-over-SSL-functionality and add it as a part of the
next official "imaplib.py"?

Here's the context diff from the most recent CVS version (1.43). It
works fine for me this way and it's only a few straight-forward lines of
code.

Maybe I could contribute a bit to the Python project with this patch?
Best regards

Tino Lange
--------------096780CBE735D0919E9873DF
Content-Type: text/plain; charset=us-ascii;
 name="imaplib.py_to_ssl_imaplib.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="imaplib.py_to_ssl_imaplib.diff"

*** imaplib_143.py	Fri Mar  1 08:52:29 2002
--- SSL_imaplib_143.py	Fri Mar  1 14:18:15 2002
***************
*** 28,33 ****
--- 28,34 ----
  CRLF = '\r\n'
  Debug = 0
  IMAP4_PORT = 143
+ IMAP4_SSL_PORT = 993
  AllowedVersions = ('IMAP4REV1', 'IMAP4')        # Most recent first
  
  #       Commands
***************
*** 980,985 ****
--- 981,1056 ----
                      i = 0
                  n -= 1
  
+ 
+ class IMAP4_SSL(IMAP4):
+ 
+     """IMAP4 client class over SSL connection
+ 
+     Instantiate with: IMAP4_SSL([, host[, port[, keyfile[, certfile]]]])
+ 
+             host - host's name (default: localhost);
+             port - port number (default: standard IMAP4 SSL port).
+             keyfile - PEM formatted file that contains your private key (default: None);
+             certfile - PEM formatted certificate chain file (default: None);
+ 
+     for more documentation see the docstring of the parent class IMAP4.
+     """
+ 
+ 
+     def __init__(self, host = '', port = IMAP4_SSL_PORT, keyfile = None, certfile = None):
+         self.keyfile = keyfile
+         self.certfile = certfile
+         IMAP4.__init__(self, host, port)
+ 
+ 
+     def open(self, host, port):
+         """Setup connection to remote server on "host:port".
+         This connection will be used by the routines:
+             read, readline, send, shutdown.
+         """
+         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+         self.sock.connect((self.host, self.port))
+         self.sslobj = socket.ssl(self.sock,self.keyfile, self.certfile) 
+ 
+ 
+     def read(self, size):
+         """Read 'size' bytes from remote."""
+         return self.sslobj.read(size)
+ 
+ 
+     def readline(self):
+         """Read line from remote."""
+         line = ""
+         while 1:
+             char = self.sslobj.read(1)
+             line += char
+             if char == "\n": return line
+ 
+ 
+     def send(self, data):
+         """Send data to remote."""
+         self.sslobj.write(data)
+ 
+ 
+     def shutdown(self):
+         """Close I/O established in "open"."""
+         self.sock.close()
+ 
+ 
+     def socket(self):
+         """Return socket instance used to connect to IMAP4 server.
+ 
+         socket = <instance>.socket()
+         """
+         return self.sock
+ 
+ 
+     def ssl(self):
+         """Return SSLObject instance used to communicate with the IMAP4 server.
+ 
+         ssl = <instance>.socket.ssl()
+         """
+         return self.sslobj
  
  
  class _Authenticator:

--------------096780CBE735D0919E9873DF--