我希望此代碼永不中斷。所以我創建了一個無限循環,並在開始時創建了一個「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