I am programming a python applet that watches the unread count of the email boxes for my workplace, and ran into an EOF error when I try to use any imaplib methods after the applet sits idle for about 10 minutes. Everything works fine until the applet has been alive for more than 10 minutes.
Here is the relevant 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's all disconnected n stuff"
loginIMAP(conn)
unreadCount = [(uc0-(uc1+uc2)),uc1,uc2]
if unreadCount[0] < 0:
unreadCount[0]=0
return unreadCount
usrEncryption
and pwdEncryption
are just me masking the u/p so our helpdesk logins aren't all public.
When I try and call the getUnread(conn)
after having the applet open for more than ten minues 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 doesnt really seem to work for the problem, which is what I really need help with. So how do I keep this connection alive and kicking?
Thanks.
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