-
-
Save obeleh/7752132 to your computer and use it in GitHub Desktop.
Original version tried to import SSLFakeFile and SSLFakeSocket which aren't present in imaplib 2.58 so I've added them manually (after simply googling them)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Python IMAP with TLS/SSL support """ | |
## | |
## Author: Alexander Brill <[email protected]> | |
## Copyright (C) 2004 Alexander Brill | |
## | |
## This program is free software; you can redistribute it and/or | |
## modify it under the terms of the GNU General Public License | |
## as published by the Free Software Foundation; either version 2 | |
## of the License, or (at your option) any later version. | |
## | |
## This program is distributed in the hope that it will be useful, | |
## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
## GNU General Public License for more details. | |
## | |
## You should have received a copy of the GNU General Public License | |
## along with this program; if not, write to the Free Software | |
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
## | |
"""USAGE: | |
import imaptls | |
imap = imaptls.IMAP4('hostname') | |
imap.starttls(keyfile=None, certfile=None) | |
# create a simple function for our PLAIN login | |
def sendAuth(response): | |
return "user\0auth\0password" | |
typ, data = imap.authenticate("PLAIN", sendAuth) | |
""" | |
import imaplib, socket | |
class SSLFakeSocket: | |
"""A fake socket object that really wraps a SSLObject. | |
It only supports what is needed in smtplib. | |
""" | |
def __init__(self, realsock, sslobj): | |
self.realsock = realsock | |
self.sslobj = sslobj | |
def send(self, str): | |
self.sslobj.write(str) | |
return len(str) | |
sendall = send | |
def close(self): | |
self.realsock.close() | |
def shutdown(self, *args, **kwargs): | |
pass | |
class SSLFakeFile: | |
"""A fake file like object that really wraps a SSLObject. | |
It only supports what is needed in smtplib. | |
""" | |
def __init__(self, sslobj): | |
self.sslobj = sslobj | |
def readline(self): | |
str = "" | |
chr = None | |
while chr != "\n": | |
chr = self.sslobj.read(1) | |
if not chr: | |
break | |
str += chr | |
return str | |
def read(self, size): | |
tmp = "" | |
prevLen = -1 | |
tmpLen = 0 | |
while tmpLen != prevLen and tmpLen != size: | |
prevLen = tmpLen | |
tmp += self.sslobj.read(size - prevLen) | |
tmpLen = len(tmp) | |
return tmp | |
def close(self): | |
pass | |
Commands = { | |
'STARTTLS': ('NONAUTH') | |
} | |
imaplib.Commands.update(Commands) | |
class IMAP4(imaplib.IMAP4): | |
def starttls(self, keyfile = None, certfile = None): | |
name = "STARTTLS" | |
typ, dat = self._simple_command(name) | |
if typ != 'OK': | |
raise self.error(dat[-1]) | |
sslobj = socket.ssl(self.sock, keyfile, certfile) | |
self.sock = SSLFakeSocket(self.sock, sslobj) | |
self.file = SSLFakeFile(sslobj) | |
cap = 'CAPABILITY' | |
self._simple_command(cap) | |
if not cap in self.untagged_responses: | |
raise self.error('no CAPABILITY response from server') | |
self.capabilities = tuple(self.untagged_responses[cap][-1].upper().split()) |
Woa this is old. I had to look what this was. Still not sure for which project I used this. But... Did you try
print(dir(socket))
Maybe the .ssl
is private now?
Well I checked and no :/
@nathan30 - imaplib in Python3 has its own starttls()
function. Wouldn't you want to use that instead if using Python 3?
https://docs.python.org/3.5/library/imaplib.html#imaplib.IMAP4.starttls
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I'm digging this.....
In python3
socket.ssl
doesnt exist. DId you have any idea to port that ?Thanks