imaplib & except

BSD UNIX bsdunix at bellatlantic.net
Wed Oct 4 18:38:48 EDT 2000


Hello all,

I'm not a programmer by trade, but am enjoying Python's efficiency and
power... it's quite amazing what it can do in just a few lines.

I've been playing with the various Internet modules, and I just can't seem
to figure this out on my own, and no amount of searching docs, deja, or
websites has turned much up.

I'm sure you guys know this in your sleep, but...

I'm trying to talk to an IMAP server using imaplib.  I can not figure out
how to catch an exception on the initial request.

I know once a connection is established that "except <instance>.error" will
work, however if the initial request never succeeds, then the <instance> is
never created--so how do I catch the exception?

Example of the error:

-----------------------------
Python 1.5.2 (#2, May  9 2000, 20:21:59)  [GCC 2.95.2 19991024 (release)] on
openbsd2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import imaplib
>>> i = imaplib.IMAP4('testhost')
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python1.5/imaplib.py", line 151, in __init__
    raise self.error(self.welcome)
imaplib.error: * BAD Sorry, we don't allow that
-----------------------------

Okay, so I see "imaplib.error" printed there so maybe I should try that:

-----------------------------
>>> try:
...     i = imaplib.IMAP4('testhost')
... except imaplib.error:
...     print 'caught'
...
Traceback (innermost last):
  File "<stdin>", line 3, in ?
AttributeError: error

-----------------------------

So "error" isn't an attribute of imaplib.  Using <instance> gives:

-----------------------------
>>> try:
...     i = imaplib.IMAP4('admin')
... except i.error:
...     print 'caught'
...
Traceback (innermost last):
  File "<stdin>", line 3, in ?
NameError: i

-----------------------------

...I assume because <instance> is never created because the request failed.

At any time *after the initial request* using <instance> works fine, as the
docs say, but not here.

So I am at a complete loss.  Could some kind soul help me out by showing me
the correct way to catch this exception?

Thanks well in advance!

bsdunix at bellatlantic.net






More information about the Python-list mailing list