Getting errors from Imaplib

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


Hi,

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.gmail.com'
try:
    imap_connection = imaplib.IMAP4(host)
    print('success')
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 CE.app/helpers/pydev/pydevd.py", line 1733, in <module>
    debugger.run(setup['file'], None, None)
  File "/Applications/Develop/PyCharm CE.app/helpers/pydev/pydevd.py", line 1226, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Applications/Develop/PyCharm CE.app/helpers/pydev/_pydev_execfile.py", 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/test.py", line 6, in <module>
    imap_connection = imaplib.IMAP4(host)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 181, in __init__
    self.open(host, port)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 257, in open
    self.sock = self._create_socket()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 247, in _create_socket
    return socket.create_connection((self.host, self.port))
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 509, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/socket.py", line 500, in create_connection
    sock.connect(sa)
OSError: [Errno 65] No route to host

Mit freundlichen Grüßen/Regards

Trixi Willius

http://www.mothsoftware.com
Mail Archiver X: The email archiving solution for professionals




More information about the Python-list mailing list