我正在编写一个python applet来监视我的工作场所的电子邮箱的未读数量,当我尝试在applet闲置大约10分钟后尝试使用任何imaplib方法时遇到了EOF错误。一切正常,直到applet活着超过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
只是我屏蔽了你的p / 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
异常块似乎并没有真正解决问题,这是我真正需要帮助的。那么我如何保持这种联系活着并且踢呢?
感谢。
答案 0 :(得分:8)
您需要通过重新初始化类重新连接,而不仅仅是登录,使用
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
答案 1 :(得分:4)
我设法将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