w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Which is the right way of recovering from a requests.exceptions.ConnectionError?

Is it really needed to logout and relogin into your session? I'd just retry the connection the same way:

def make_soup(session,url):
    success = False
    response = None
    for attempt in range(1, MAXTRIES):
        try:
            response = session.get(url)
            # If session.get succeeded, we break out of the
            # for loop after setting a success flag
            success = True
            break
        except requests.exceptions.ConnectionError as req_ce:
            error_msg = req_ce.args[0].reason.strerror
            print "Error: %s con la url %s" % (error_msg, url)
            print " Attempt %s of %s" % (attempt, MAXTRIES)
            sleep(randint(MIN_SLEEPTIME, MAX_SLEEPTIME))

    # Figure out if we were successful. 
    # Note it may not be needed to have a flag, you can maybe just
    # check the value of response here.
    if not success:
        print "Couldn't get it after retrying many times"
        return None

    #Once we get here, we know we got a good response
    soup = BeautifulSoup(response.text)
    return soup




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.