EOF Error in Imaplib

你离开我真会死。 提交于 2019-12-03 08:02:44

You need to reconnect by re-initialize class, not just login, using

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

A complete example:

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

I managed to integrate cxase's into a custom imap class that took care of all my problems. Here is the code for anyone reading this:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!