此代码:
import imaplib, re
import os
import time
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("ddd@ddd.com", "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
我不明白为什么它会随机崩溃。
答案 0 :(得分:4)
最终错误行是描述性的:连接在IMAP操作完成之前超时。可能只是Gmail运行缓慢。如果是这种情况,您可以尝试延长超时:
import socket
socket.setdefaulttimeout(15) # In seconds
其中15
是您通过实验确定的值适合您。