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('something@myserver.com', 'mypassword')
('OK', ['something@myserver.com Joe Smith authenticated (Success)'])
(Of course you don't have to rebind the same name imap_server
to the new connection.)