8

我正在编写一个 python 小程序,用于监视我工作场所的未读邮箱数量,当我在小程序闲置约 10 分钟后尝试使用任何 imaplib 方法时遇到 EOF 错误。一切正常,直到小程序存活超过 10 分钟。

这是 imaplib 对象的相关代码。

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)

def loginIMAP (imapObj):
    # Login to Helpdesk Google Apps Email account using encryption
    imapObj.login(base64.b64decode("usrEncryption"), base64.b64decode("pwdEncrytion"))
    return(getUnread(imapObj))

def closeIMAP (imapObj):
    imapObj.logout()


def getUnread (imapObj):
    # Check connection status OK
    try:   
        uc0 = int(re.search("UNSEEN (\d+)", imapObj.status("INBOX", "(UNSEEN)")[1][0]).group(1))
        uc1 = int(re.search("UNSEEN (\d+)", imapObj.status("A box 1", "(UNSEEN)")[1][0]).group(1))
        uc2 = int(re.search("UNSEEN (\d+)", imapObj.status("A box 2", "(UNSEEN)")[1][0]).group(1))
    except:
        print "Shit's all disconnected n stuff"
        loginIMAP(conn)

    unreadCount = [(uc0-(uc1+uc2)),uc1,uc2]
    if unreadCount[0] < 0:
        unreadCount[0]=0
    return unreadCount

usrEncryptionpwdEncryption只是我屏蔽了 u/p,所以我们的帮助台登录信息并不都是公开的。

当我getUnread(conn)在打开小程序十分钟后尝试调用时,我得到以下输出:

    Traceback (most recent call last):
  File "C:\Jamin'sApps\Development\Projects\Check HD Box\checkHDBox.py", line 255, in OnRefresh
    unread = getUnread(conn)
  File "C:\Jamin'sApps\Development\Projects\Check HD Box\checkHDBox.py", line 119, in getUnread
    uc0 = int(re.search("UNSEEN (\d+)", imapObj.status("INBOX", "(UNSEEN)")[1][0]).group(1))
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 710, in status
    typ, dat = self._simple_command(name, mailbox, names)
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: STATUS => socket error: EOF
Traceback (most recent call last):
  File "C:\Jamin'sApps\Development\Projects\Check HD Box\checkHDBox.py", line 255, in OnRefresh
    unread = getUnread(conn)
  File "C:\Jamin'sApps\Development\Projects\Check HD Box\checkHDBox.py", line 119, in getUnread
    uc0 = int(re.search("UNSEEN (\d+)", imapObj.status("INBOX", "(UNSEEN)")[1][0]).group(1))
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 710, in status
    typ, dat = self._simple_command(name, mailbox, names)
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Jamin'sApps\Development\Python\python2.7.2\lib\imaplib.py", line 859, in _command
    raise self.abort('socket error: %s' % val)
imaplib.abort: socket error: [Errno 10053] An established connection was aborted by the software in your host machine

异常块似乎并没有真正解决这个问题,这是我真正需要帮助的。那么我如何保持这种联系活跃和活跃呢?

谢谢。

4

2 回答 2

14

您需要通过重新初始化类重新连接,而不仅仅是登录,使用

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)

一个完整的例子:

while True:
    imap = imaplib.IMAP4_SSL(SERVER)
    r, d = imap.login(ACCOUNT, PASSWORD)
    assert r == 'OK', 'login failed'
    try:
        # do things with imap
    except imap.abort, e:
        continue
    imap.logout()
    break
于 2011-09-28T10:25:25.283 回答
6

我设法将 cxase 集成到一个自定义 imap 类中,该类解决了我所有的问题。这是任何阅读此内容的人的代码:

class IMAPConnection():

    def __init__(self):
        self.imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)

    def login (self):
        # Login to Helpdesk Google Apps Email account using encryption
        self.imap.login(base64.b64decode("username"), base64.b64decode("password"))

    def logout (self):
        self.imap.logout()

    def getUnread (self):
        # Check connection status OK
        try:   
            uc0 = int(re.search("UNSEEN (\d+)", self.imap.status("INBOX", "(UNSEEN)")[1][0]).group(1))
            uc1 = int(re.search("UNSEEN (\d+)", self.imap.status("A box 1", "(UNSEEN)")[1][0]).group(1))
            uc2 = int(re.search("UNSEEN (\d+)", self.imap.status("A box 2", "(UNSEEN)")[1][0]).group(1))
        except imap.abort:

            # Reinstantiate connection and login
            self.imap = imaplib.IMAP4_SSL("imap.gmail.com", 993)
            self.login()

            # Retry unread update block
            uc0 = int(re.search("UNSEEN (\d+)", self.imap.status("INBOX", "(UNSEEN)")[1][0]).group(1))
            uc1 = int(re.search("UNSEEN (\d+)", self.imap.status("A box 1", "(UNSEEN)")[1][0]).group(1))
            uc2 = int(re.search("UNSEEN (\d+)", self.imap.status("A box 2", "(UNSEEN)")[1][0]).group(1))

        # Is the Helpdesk Negative? Hell no it's not.
        unreadCount = [(uc0-(uc1+uc2)),uc1,uc2]
        if unreadCount[0] < 0:
            unreadCount[0]=0
        return unreadCount
于 2011-09-29T18:51:52.537 回答