我正在编写一个 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
usrEncryption
pwdEncryption
只是我屏蔽了 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
异常块似乎并没有真正解决这个问题,这是我真正需要帮助的。那么我如何保持这种联系活跃和活跃呢?
谢谢。