Cannot connect to IMAP server in Python 3.2

Steve Howell showell30 at yahoo.com
Thu Apr 5 09:45:50 EDT 2012


On Apr 5, 5:25 am, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Thu, 05 Apr 2012 12:16:09 +0000, Steven D'Aprano wrote:
> > On Thu, 05 Apr 2012 00:21:31 -0700, Steve Howell wrote:
> >> Why are you changing the invocation between versions of Python?
>
> > Because imaplib.IMAP4_SSL apparently no longer exists in Python 3.
>
> >>>> server = imaplib.IMAP4_SSL('xxxxx')
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > AttributeError: 'module' object has no attribute 'IMAP4_SSL'
>
> Wait a minute...
>
> IMAP4_SSL is documented as existing in Python 3. And when I run Python
> 3.2 on a Centos machine, instead of Debian, it includes IMAP4_SSL which
> works fine.
>
> [steve at ando ~]$ python3.2
> Python 3.2.2 (default, Mar  4 2012, 10:50:33)
> [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> py> import imaplib
> py> server = imaplib.IMAP4_SSL('xxxxx')
> py> server
> <imaplib.IMAP4_SSL object at 0xb7b8632c>
>
> So there's something screwy going on here. Why does my Python 3.2 on
> Debian not include IMAP4_SSL, but Python 2.6 does?
>

Looking at the source can give you some insight:

http://hg.python.org/cpython/file/3.2/Lib/imaplib.py

Look for where IMAP4_SSL is defined.  On your 3.2/Debian setup, the
HAVE_SSL flag (see two lines above the "class" statement) is
apparently false:

  1174 if HAVE_SSL:
  1175
  1176     class IMAP4_SSL(IMAP4):

If you look for the first mention of HAVE_SSL, it becomes apparent
that you have some issue with importing the ssl module, which
unfortunately gets buried fairly silently by imaplib:

    27 try:
    28     import ssl
    29     HAVE_SSL = True
    30 except ImportError:
    31     HAVE_SSL = False

Damien, in his response, asks you to try "import ssl" on your Debian
machine.  I think he's on the right track in identifying the problem,
based on the simple code above.

Once you are able to import ssl, you should be able to use IMAP4_SSL,
but that still doesn't entirely explain to me why you got a timeout
error with plain IMAP4 and the proper port.  (I would have expected a
failure, but of a different kind.)

I'd still be curious to see what happens when you try this:

   import socket, imaplib
   your_host_name = # ...
   socket.create_connection((your_host_name, imaplib.IMAP4_SSL_PORT))



More information about the Python-list mailing list