0

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

1
  • 1
    Put a while loop with a try/except around the code beginning with 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. Commented Jun 21, 2011 at 19:25

3 Answers 3

4
import imaplib, re
import os
import time

while True:
    try:
        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)
    except HypotheticalException:
        pass
Sign up to request clarification or add additional context in comments.

2 Comments

it's probably not desirable to have the outer loop run without bound. I would recommend a max retries value to control this.
@AJ: As most books generally say "That is left as an exercise to the reader"
0

Use try ... except

try:
   unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
   if int(unreadCount) > 10:
            print "restarting..."

    time.sleep(50)
except Exception:
  pass

Comments

0

Try this:

import imaplib, re
import os
import time


for n in range(3):
try:
    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)
    break
except Exception, e:
    if n == 2:
        print >>sys.stderr, "Failure During processing, restarting..."
        print >>sys.stderr, e

You can set n to be however many tries you want to allow.

EDIT: Hmm, after further perusal, it seems I got your code slightly wrong. I've edited and fixed my version. You will need to adjust and edit your while loop, as I am not entirely sure what it is you are up to.

EDIT 2: Since you need to reconnect and try again, I've moved the conn section within the try block.

3 Comments

Allow you mean running the code again? I don't quite understand what+s the point of the "n".
The break will drop out of the for loop upon success. If you fail, n is checked. If it's less than 2, print the error and retry. Otherwise, quit trying.
The "conn" must be inside the try, because I need to connect again if something is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.