ssl_context = create_default_context()
try:
- newsock = ssl_context.wrap_socket(sock, server_hostname=hostname)
+ return ssl_context.wrap_socket(sock, server_hostname=hostname)
except ssl.CertificateError:
sock.shutdown()
sock.close()
raise imaplib.Error("server certificate not valid for %s" % hostname)
- return _SSLSocketWithShutdown(newsock)
-
class IMAP4_TLS(imaplib.IMAP4):
"""IMAP4 client class for TLS/SSL connections.
self.sock = wrap_socket(sock, self.ssl_context, host)
self.file = self.sock.makefile('rb')
+ def read(self, size):
+ return self.file.read(size)
-# TODO: get shutdown added in backports.ssl.SSLSocket
-class _SSLSocketWithShutdown(object):
-
- def __init__(self, sslsock):
- self.sslsock = sslsock
+ def readline(self):
+ return self.file.readline()
- def shutdown(self, _):
- return self.sslsock._conn.shutdown()
+ def send(self, data):
+ remaining = len(data)
+ while remaining > 0:
+ sent = self.sock.send(data)
+ if sent == remaining:
+ break
+ data = data[sent:]
+ remaining -= sent
- def __getattr__(self, name):
- return getattr(self.sslsock, name)
+ def shutdown(self):
+ self.file.close()
+ self.sock.close()