我希望此代码永不中断。所以我创建了一个无限循环,并在开始时创建了一个“goto”。但是,它仍然无法正常工作。Python:始终运行程序
[email protected]:~# cat gmail2.py
import imaplib, re
import os
import time
import socket
socket.setdefaulttimeout(60)
def again():
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("[email protected]", "xxx")
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 sss-xxxx-203')
#os.system('ls')
#print "Restarting server...."
time.sleep(60)
again()
1
Traceback (most recent call last):
File "gmail2.py", line 22, in <module>
again()
File "gmail2.py", line 12, in again
unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
File "/usr/lib/python2.6/imaplib.py", line 703, in status
typ, dat = self._simple_command(name, mailbox, names)
File "/usr/lib/python2.6/imaplib.py", line 1060, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib/python2.6/imaplib.py", line 890, in _command_complete
raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: STATUS => socket error: EOF
您的连接在您的搜索调用中没有收到任何数据,或者它到达文件末尾,这就是它返回套接字错误的原因。在调用re.search()之前,可能会添加EOF检查。此外,通常无限循环是危险的,您可能想要添加某种破坏条件。 – kand 2012-02-16 19:53:16
或者发现异常。 – MRAB 2012-02-16 19:55:48