在我正在编程的应用程序中,我目前正在使用 IMAP 的搜索功能来获取电子邮件 ID,这很好,因为这是一项简单易行的任务,但是我想知道随着服务器变得越来越满,这是否会损害搜索速度(即目前速度很快),如果是这样,则值得处理 IMAP 空闲命令和 twisted.internet.mail。
我已经通过这个实现了 IDLE
class Command(object):
_1_RESPONSES = ('CAPABILITY', 'FLAGS', 'LIST', 'LSUB', 'STATUS', 'SEARCH', 'NAMESPACE')
_2_RESPONSES = ('EXISTS', 'EXPUNGE', 'FETCH', 'RECENT')
_OK_RESPONSES = ('UIDVALIDITY', 'UNSEEN', 'READ-WRITE', 'READ-ONLY', 'UIDNEXT', 'PERMANENTFLAGS')
defer = None
def __init__(self, command, args=None, wantResponse=(),
continuation=None, *contArgs, **contKw):
self.command = command
self.args = args
self.wantResponse = wantResponse
self.continuation = lambda x: continuation(x, *contArgs, **contKw)
self.lines = []
def format(self, tag):
if self.args is None:
return ' '.join((tag, self.command))
return ' '.join((tag, self.command, self.args))
def finish(self, lastLine, unusedCallback):
send = []
unuse = []
for L in self.lines:
names = parseNestedParens(L)
N = len(names)
if (N >= 1 and names[0] in self._1_RESPONSES or
N >= 2 and names[1] in self._2_RESPONSES or
N >= 1 and names[0] in self.wantResponse or # allows for getting the responses you want, twisted doesn't seem to do that at least with the idle command
N >= 2 and names[1] in self.wantResponse or # same as above line just with 2_RESPONSES check
N >= 2 and names[0] == 'OK' and isinstance(names[1], types.ListType) and names[1][0] in self._OK_RESPONSES):
send.append(names)
else:
unuse.append(names)
d, self.defer = self.defer, None
d.callback((send, lastLine))
if unuse:
unusedCallback(unuse)
正在发送空闲命令
cmd = Command("IDLE", continuation = self.a)
d = self.imap_connection.sendCommand(cmd)
return d
现在我对 IDLE 犹豫的原因首先是如果服务器不支持它我就不能使用它(尽管这不是主要原因),我也不希望是因为空闲命令是未标记的响应以及如何知道它们是针对空闲命令的。