为什么我无法在Python中两次登录imap服务器 [英] Why can't I login to an imap server twice in Python

查看:151
本文介绍了为什么我无法在Python中两次登录imap服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如以下错误消息所述,我无法登录,因为我处于LOGOUT状态而不是NONAUTH状态.如何从LOGOUT到NONAUTH?

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('something@myserver.com', 'mypassword')
('OK', ['something@myserver.com Joe Smith authenticated (Success)'])
>>> imap_server.logout()
('BYE', ['LOGOUT Requested'])
>>> imap_server.login('something@myserver.com', '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()

推荐答案

您要执行的操作在IMAP中是非法的.如果您阅读 RFC 3501 ,它将明确定义

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)'])

(当然,您不必将相同名称的imap_server重新绑定到新连接.)

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

这篇关于为什么我无法在Python中两次登录imap服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆