Python:检查电子邮件时代码随机崩溃

发布于 2024-12-27 21:12:47 字数 2129 浏览 0 评论 0原文

此代码:

import imaplib, re
import os
import time

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("[email protected]", "ddd")

while(True):
    unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
    print unreadCount

    if int(unreadCount) > 20:
      os.system('heroku restart --app warm-beach-203')
        #os.system('ls')
        #print "Restarting server...."

    time.sleep(60)

输出:

0
1
0
0
0
0
0
4
Traceback (most recent call last):
  File "gmail.py", line 10, in <module>
    unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 704, in status
    typ, dat = self._simple_command(name, mailbox, names)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1059, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 889, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 990, in _get_tagged_response
    self._get_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 907, in _get_response
    resp = self._get_line()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1000, in _get_line
    line = self.readline()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1170, in readline
    char = self.sslobj.read(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ssl.py", line 136, in read
    return self._sslobj.read(len)
socket.error: [Errno 60] Operation timed out

我不明白为什么它会随机崩溃。

This code:

import imaplib, re
import os
import time

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("[email protected]", "ddd")

while(True):
    unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
    print unreadCount

    if int(unreadCount) > 20:
      os.system('heroku restart --app warm-beach-203')
        #os.system('ls')
        #print "Restarting server...."

    time.sleep(60)

Output:

0
1
0
0
0
0
0
4
Traceback (most recent call last):
  File "gmail.py", line 10, in <module>
    unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 704, in status
    typ, dat = self._simple_command(name, mailbox, names)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1059, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 889, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 990, in _get_tagged_response
    self._get_response()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 907, in _get_response
    resp = self._get_line()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1000, in _get_line
    line = self.readline()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/imaplib.py", line 1170, in readline
    char = self.sslobj.read(1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ssl.py", line 136, in read
    return self._sslobj.read(len)
socket.error: [Errno 60] Operation timed out

I dont understand why it crashes randomly.

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

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

发布评论

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

评论(1

吃→可爱长大的 2025-01-03 21:12:47

最后的错误行是描述性的:在 IMAP 操作完成之前连接超时。可能只是 Gmail 运行缓慢。如果是这种情况,您可以尝试延长超时时间,如下所示:

import socket
socket.setdefaulttimeout(15)  # In seconds

其中 15 是您通过实验确定的最适合您的值。

The final error line is descriptive: the connection is timing out before the IMAP operation completes. It could be simply that Gmail is running slowly. If that's the case, you could try extending the timeout like so:

import socket
socket.setdefaulttimeout(15)  # In seconds

where 15 is the value you've experimentally determined works well for you.

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