Search code examples
pythontimertry-catch

python Time a try except


My problem is very simple. I have a try/except code. In the try I have some http requests attempts and in the except I have several ways to deal with the exceptions I'm getting.

Now I want to add a time parameter to my code. Which means the try will only last for 'n' seconds. otherwise catch it with except.

In free language it would appear as:

try for n seconds:
    doSomthing()
except (after n seconds):
    handleException()

this is mid-code. Not a function. and I have to catch the timeout and handle it. I cannot just continue the code.

        while (recoveryTimes > 0):
            try (for 10 seconds):

                urllib2.urlopen(req)
                response = urllib2.urlopen(req)     
                the_page = response.read()
                recoveryTimes = 0

            except (urllib2.URLError, httplib.BadStatusLine) as e:
                print str(e.__unicode__())
                print sys.exc_info()[0]
                recoveryTimes -= 1

                if (recoveryTimes > 0):
                    print "Retrying request. Requests left %s" %recoveryTimes
                    continue
                else:
                    print "Giving up request, changing proxy."
                    setUrllib2Proxy()
                    break
            except (timedout, 10 seconds has passed)
                setUrllib2Proxy()
                break

The solution I need is for the try (for 10 seconds) and the except (timeout, after 10 seconds)


Solution

  • Check the documentation

    import urllib2
    request = urllib2.Request('http://www.yoursite.com')
    try:
        response = urllib2.urlopen(request, timeout=4)
        content = response.read()
    except urllib2.URLError, e:
        print e
    

    If you want to catch more specific errors check this post

    or alternatively for requests

    import requests
    try:
        r = requests.get(url,timeout=4)
    except requests.exceptions.Timeout as e:
        # Maybe set up for a retry
        print e
    
    except requests.exceptions.RequestException as e:
        print e
    

    More about exceptions while using requests can be found in docs or in this post