Imaplib 中的 EOF 错误
Posted
技术标签:
【中文标题】Imaplib 中的 EOF 错误【英文标题】:EOF Error in Imaplib 【发布时间】:2011-11-26 10:01:57 【问题描述】:我正在编写一个 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
异常块似乎并没有真正解决问题,这是我真正需要帮助的地方。那么如何保持这种连接的活跃和有效呢?
谢谢。
【问题讨论】:
听起来像是超时了。但默认情况下,imaplib 中的socket
没有设置超时,所以也许是 Gmail 做的?使用不同的 SMTP 服务器会得到不同的结果吗?
【参考方案1】:
您需要通过重新初始化类来重新连接,而不仅仅是登录,使用
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
【讨论】:
感谢您的回复。异常中的“e”是什么意思?e
仅用于表示异常实例,您可以在 except
块中执行 log.error(str(e))
【参考方案2】:
我设法将 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
【讨论】:
以上是关于Imaplib 中的 EOF 错误的主要内容,如果未能解决你的问题,请参考以下文章
使用 imaplib 阅读电子邮件 - “超过 10000 字节”错误
imaplib , imap_tools 几分钟后返回错误。 (imaplib.abort:命令:SELECT => IMAP4rev1 服务器注销)
imaplib2:imap.gmail.com 处理程序 BYE 响应:系统错误