I have this code:
import imaplib, re
import os
import time
conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("ddd", "dddd")
while(True):
unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
print unreadCount
if int(unreadCount) > 10:
print "restarting..."
time.sleep(50)
Which sometimes loses the connection and stops working. How can I catch the exception and start the code over every time it breaks?
Thanks
conn = ..., and in the except block continue in your loop. Make sure you implement some max retries (e.g.while(retries<n)). BTW, I would put this as an answer but I've exceeded rep cap for the day already. I'll let someone else have the rep.