Is it really needed to logout and relogin into your session? I'd just
retry the connection the same way:
success = False
response = None
for attempt in range(1, MAXTRIES):
response = session.get(url)
# If session.get succeeded, we break out of the
# for loop after setting a success flag
success = True
except requests.exceptions.ConnectionError as req_ce:
error_msg = req_ce.args.reason.strerror
print "Error: %s con la url %s" % (error_msg, url)
print " Attempt %s of %s" % (attempt, MAXTRIES)
# 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"
#Once we get here, we know we got a good response
soup = BeautifulSoup(response.text)