Imaplib 中的 EOF 错误

发布于 2024-12-06 13:55:01 字数 3114 浏览 0 评论 0原文

我正在编写一个 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

usrEncryptionpwdEncryption 只是我屏蔽 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

异常块似乎并不能真正解决该问题,这才是我真正想要的需要帮助。那么我该如何保持这种联系的活力呢?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-12-13 13:55:01

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

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

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
风向决定发型 2024-12-13 13:55:01

我设法将 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

I managed to integrate cxase's into a custom imap class that took care of all my problems. Here is the code for anyone reading this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文