Getting errors from Imaplib

Beatrix Willius from Moth Software mail at
Sun Nov 30 09:36:33 CET 2014


first poster here. I still consider myself pretty much a Python newbie.

Let's say I have the following very simple Python code:

import imaplib

host = ''
    imap_connection = imaplib.IMAP4(host)
except imaplib.IMAP4.error:
    print('authentication failed')

Now, Gmail doesn't like non-ssl connections. In this case I only want to return an error. This worked for Pyhton 2.7 but in Python 3.4 I only get a traceback (see below). The line print('authentication failed') is not executed at all. How do I capture the error for using the print function?

Mac OS 10.10. Python 3.4.1. The script is part of a larger one that is talking to my Xojo application.

Connected to pydev debugger (build 135.1057)
Traceback (most recent call last):
  File "/Applications/Develop/PyCharm", line 1733, in <module>['file'], None, None)
  File "/Applications/Develop/PyCharm", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/Develop/PyCharm", line 38, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
  File "/Users/beatrixwillius/Documents/ Datei/Development/Mail Archiver/Projects/MainClasses Projects/imap/test/", line 6, in <module>
    imap_connection = imaplib.IMAP4(host)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 181, in __init__, port)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 257, in open
    self.sock = self._create_socket()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 247, in _create_socket
    return socket.create_connection((, self.port))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 509, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/", line 500, in create_connection
OSError: [Errno 65] No route to host

Mit freundlichen Grüßen/Regards

Trixi Willius
Mail Archiver X: The email archiving solution for professionals

More information about the Python-list mailing list