imaplib: how to specify SSL/TLS protocol version?

Grant Edwards invalid at invalid.invalid
Wed Apr 9 17:10:10 EDT 2014


On 2014-04-09, Grant Edwards <invalid at invalid.invalid> wrote:
> On 2014-04-09, Tim Chase <python.list at tim.thechases.com> wrote:
>> On 2014-04-09 20:20, Grant Edwards wrote:
>>> I'm not too keen on this approach, but monkey-patching the open()
>>> method seems to work:
>>> 
>>> def my_imap4_ssl_open(self, host = '', port = 993):
>>>     self.host = host
>>>     self.port = port
>>>     self.sock = socket.create_connection((host, port))
>>>     self.sslobj = ssl.wrap_socket(self.sock, self.keyfile,
>>> self.certfile, ssl_version=ssl.PROTOCOL_TLSv1) self.file =
>>> self.sslobj.makefile('rb')
>>> 
>>> imaplib.IMAP4_SSL.open = my_imap4_ssl_open 
>>
>> Our messages passed in the ether.
>
> Yep saw that.  Thanks for the answers. 
>
>> You don't have to feel dirty for monkey-patching, as you can just do
>> it with inheritance.
>
> Doh. I don't know why I didn't think of that...

Now I remember...

I left out a relevent fact: I'm not the one calling IMAP4_<whatever>.

That's being done by the imapclient library.  There's no way to pass
imapclient a custom class to use.  It's hard-waired to call either
imaplib.IMAP4_stream(), imaplib.IMAP4(), or imaplib.IMAP4_SSL().  I
could create an IMAP4_TLS1 class, but I would then have to sub-class
imapclient.IMAPClient and override its _create_IMAP4() method to make
it call my IMAP4_TLS1() class instead of calling imaplib.IMAP4_SSL().

Monkey-patching imaplib seems a little better since it it doesn't
depend on assumptions about the internal workings of imapclient (other
than the fact that it uses imaplib.IMAP4_SSL).

-- 
Grant Edwards               grant.b.edwards        Yow! MMM-MM!!  So THIS is
                                  at               BIO-NEBULATION!
                              gmail.com            



More information about the Python-list mailing list