Imaplib中的EOF错误 [英] EOF Error in Imaplib

查看:580
本文介绍了Imaplib中的EOF错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Python小程序,该程序监视我的工作场所中电子邮件框的未读计数,当小程序闲置约10分钟后尝试使用任何imaplib方法时,遇到了EOF错误。一切正常,直到小程序存活超过10分钟为止。

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.

以下是imaplib对象的相关代码。

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 pwdEncryption 只是我掩盖了u / p,所以我们的帮助台登录并非全部公开。

usrEncryption and pwdEncryption are just me masking the u/p so our helpdesk logins aren't all public.

当小程序打开超过十分钟后,我尝试调用 getUnread(conn)时,我得到以下输出:

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?

谢谢。

推荐答案

您需要通过重新初始化类来重新连接,而不仅仅是登录,使用

You need to reconnect by re-initialize class, not just login, using

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

这篇关于Imaplib中的EOF错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆