3

嗨,我突然在一些以前运行良好的代码上使用 imaplib 时遇到错误。

import imaplib
m = imaplib.IMAP4('myserver','port')
m.login(r'username','password')
m.select()

给我错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/imaplib.py", line 649, in select
    typ, dat = self._simple_command(name, mailbox)
  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 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => unexpected response: '*  1520 EXISTS'

我不确定这意味着什么。电子邮件通过其他方式很好,我使用 davmail 作为服务器。

该程序将具有特定名称的附件保存在特定文件夹中。

我已经穿过它,它绝对m.select()是它摔倒的地方。

直到最近,同样的程序运行良好。

我做错了什么,我该如何解决?

活动日志如下

>>> import imaplib
>>> m = imaplib.IMAP4('server','port')
>>> Debug=4
>>> m.debug
0
>>> m.debug=4
>>> m.debug
4
>>> m.login(r'username','password')
  01:26.55 > HLFI1 LOGIN "username" "password"
  01:30.76 < HLFI1 OK Authenticated
('OK', ['Authenticated'])
>>> m.list()
  01:56.33 > HLFI2 LIST "" *
  02:00.04 < * LIST (\HasNoChildren) "/" "Trash/Sent Messages"
  02:00.04 < * LIST (\HasNoChildren) "/" "Sync Issues/Server Failures"
  02:00.04 < * LIST (\HasNoChildren) "/" "Sync Issues/Local Failures"
  02:00.04 < * LIST (\HasNoChildren) "/" "Sync Issues/Conflicts"
  02:00.04 < * LIST (\HasChildren) "/" "Sync Issues"
  02:00.04 < * LIST (\HasNoChildren) "/" "Junk E-mail"
  02:00.04 < * LIST (\HasNoChildren) "/" "Drafts"
  02:00.04 < * LIST (\HasChildren) "/" "Trash"
  02:00.04 < * LIST (\HasNoChildren) "/" "Sent"
  02:00.04 < * LIST (\HasNoChildren) "/" "Outbox"
  02:00.04 < * LIST (\HasNoChildren) "/" "INBOX"
  02:00.04 < HLFI2 OK LIST completed
('OK', ['(\\HasNoChildren) "/" "Trash/Sent Messages"', '(\\HasNoChildren) "/" "Sync Issues/Server Failures"', '(\\HasNoChildren) "/" "Sync Issues/Local Failures"', '(\\HasNoChildren) "/" "Sync Issues/Conflicts"', '(\\HasChildren) "/" "Sync Issues"', '(\\HasNoChildren) "/" "Junk E-mail"', '(\\HasNoChildren) "/" "Drafts"', '(\\HasChildren) "/" "Trash"', '(\\HasNoChildren) "/" "Sent"', '(\\HasNoChildren) "/" "Outbox"', '(\\HasNoChildren) "/" "INBOX"'])
>>> m.select()
  02:21.37 > HLFI3 SELECT INBOX
  02:30.87 < *  1548 EXISTS
  02:30.87 last 4 IMAP4 interactions:
  00:16.73 < * OK [CAPABILITY IMAP4REV1 AUTH=LOGIN MOVE] IMAP4rev1 DavMail 4.3.0-2125 server ready
  00:16.73 > HLFI0 CAPABILITY
  00:16.74 < * CAPABILITY IMAP4REV1 AUTH=LOGIN MOVE
  00:16.77 < HLFI0 OK CAPABILITY completed
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/imaplib.py", line 649, in select
    typ, dat = self._simple_command(name, mailbox)
  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 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => unexpected response: '*  1548 EXISTS'

** 更新 **

我现在在 python-dev 下提交了一个错误

关于 Python 的错误报告

David Murray 说响应不符合 RFC

还有第二个在 davmail sourceforge 下

davmail 错误报告

M guessant 说 IMAP 必须保持活动状态。

我会随着事态的发展保持更新..

4

2 回答 2

2

It appears that the space-padded message count in the RECENT response is what triggers this. It is unclear to me whether it should be classified as an error in Python's imaplib or in the IMAP server you are using. I would argue that imaplib should be robust against this, regardless of what the spec says. Perhaps you should file a bug report?

(If you do, please take care to add details about which server is producing this response. If it is a commercial product with a respectable market share, it is important to fix, whereas of course, if it's your own simple Python server, they might not care.)

于 2013-06-04T09:40:17.990 回答
2

发现EXISTS命令的响应是用 填充的davmail,好像是邮件数量在 500 左右或者超过 500 条的时候。

可接受的回应:

  58:24.54 > PJJD3 EXAMINE INBOX
  58:24.77 < * 486 EXISTS
  58:24.78      matched r'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?' => ('486', 'EXISTS', None, None)
  58:24.78 untagged_responses[EXISTS] 0 += ["486"]

响应失败:

  57:50.86 > KPFE3 EXAMINE INBOX
  57:51.10 < *  953 EXISTS
  57:51.10 last 0 IMAP4 interactions:
  57:51.10 > KPFE4 LOGOUT

已在githubimaplib上为库创建拉取请求以解决该问题

要修补您的代码以imaplib允许更改正则表达式,只需将以下内容添加到您的代码中:

imaplib.Untagged_status = imaplib.re.compile(br'\*[ ]{1,2}(?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?')
于 2019-06-21T04:56:58.713 回答