10

As the error message below states, I cannot log in because I'm in state LOGOUT and not in state NONAUTH. How do I get from LOGOUT to NONAUTH?

Example below (obviously the login credentials are faked below)

Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import imaplib
>>> imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
>>> imap_server.login('[email protected]', 'mypassword')
('OK', ['[email protected] Joe Smith authenticated (Success)'])
>>> imap_server.logout()
('BYE', ['LOGOUT Requested'])
>>> imap_server.login('[email protected]', 'mypassword')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/imaplib.py", line 505, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 825, in _command
    ', '.join(Commands[name])))
imaplib.error: command LOGIN illegal in state LOGOUT, only allowed in states NONAUTH
>>> quit()

1 Answer 1

13

What you're trying to do is illegal in IMAP. If you read over RFC 3501, it explicitly defines Logout State as a state from which there is no return. Whether you get an error from imaplib itself, or from the server, or you get really unlucky and it works and takes you into undefined-behavior territory… the answer is the same: don't do it.

So, you have to create a new connection to the server to login again:

>>> imap_server.logout()
('BYE', ['LOGOUT Requested'])
>>> imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
>>> imap_server.login('[email protected]', 'mypassword')
('OK', ['[email protected] Joe Smith authenticated (Success)'])

(Of course you don't have to rebind the same name imap_server to the new connection.)

3
  • Thanks! That's exactly it!
    – dl__
    Apr 19, 2013 at 20:18
  • While you explained that LOGOUT state is a state from which there is no return, you didn't exactly explain how the OP go into a state of NONAUTH - is that the state you are in when make an IMAP connection? That is, after this imaplib.IMAP4_SSL("imap.gmail.com",993)? But you cannot get to NONAUTH state from any other state? I don't see any reference in RFC3501.
    – Devy
    Dec 19, 2013 at 19:44
  • @Devy: I didn't explain the entire state machine from RFC 3501 because RFC 3501 already explains it pretty well. Section 3 says, "The initial state is identified in the server greeting." Section 3.1 says, "Not Authenticated State … is entered when a connection starts unless the connection has been pre-authenticated." Otherwise, "Authenticated State … is entered when a pre-authenticated connection starts". After 3.4 there's an ASCII-ART diagram that shows all the ways to get to each state. There is no way to get back to Not Authenticated if you leave it.
    – abarnert
    Dec 19, 2013 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.