Python:检查电子邮件时代码随机崩溃
此代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后的错误行是描述性的:在 IMAP 操作完成之前连接超时。可能只是 Gmail 运行缓慢。如果是这种情况,您可以尝试延长超时时间,如下所示:
其中
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:
where
15
is the value you've experimentally determined works well for you.