# HG changeset patch # User "" <;> # Date 1228214012 -3600 # Branch trunk # Node ID 16f52cec9334b5fe830124224752a6caa2fb9452 # Parent 35e81467ab0d1d1f560bfa89cb7ad9777a56473a Implement STLS command. diff --git a/Lib/poplib.py b/Lib/poplib.py --- a/Lib/poplib.py +++ b/Lib/poplib.py @@ -14,6 +14,12 @@ # Imports import re, socket + +try: + import ssl + HAVE_SSL = True +except ImportError: + HAVE_SSL = False __all__ = ["POP3","error_proto"] @@ -333,11 +339,25 @@ raise error_proto('-ERR CAPA not supported by server') return caps -try: - import ssl -except ImportError: - pass -else: + def stls(self, keyfile = None, certfile = None): + if not HAVE_SSL: + raise error_proto('-ERR TLS support missing') + if hasattr(self,'_tls_established') and self._tls_established: + raise error_proto('-ERR TLS session already established') + caps=self.capa() + if not 'STLS' in caps: + raise error_proto('-ERR STLS not supported by server') + try: + resp=self._shortcmd('STLS') + self.sock=ssl.wrap_socket(self.sock, keyfile, certfile) + self.file=self.sock.makefile('rb') + self._tls_established = True + except error_proto, val: + resp = val + return resp + + +if HAVE_SSL: class POP3_SSL(POP3): """POP3 client class over SSL connection