EOF Error in Imaplib

I am programming a python applet that keeps track of an unread number of mailboxes for my workstation and encounters an EOF error when I try to use any imaplib methods after the applet has been idle for about 10 minutes. Everything worked fine until the applet was alive for more than 10 minutes.

Here is the appropriate code for the imaplib object.

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 all disconnected n stuff"
        loginIMAP(conn)

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

usrEncryptionand pwdEncryptionis just a u / p disguise, so our support accounts are not publicly available.

When I try to call getUnread(conn)after opening the applet for more than ten minutes, I get the following output:

    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

The exception block really doesn't work for the problem, and this is what I really need. So how can I keep this connection and kick?

Thank.

+5
2

, ,

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
+7

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 not.
        unreadCount = [(uc0-(uc1+uc2)),uc1,uc2]
        if unreadCount[0] < 0:
            unreadCount[0]=0
        return unreadCount
+3

All Articles