Reputation: 369
So basically i'm using requests where I sometimes enter a URL and gives me an error saying:
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
which is fine when I do a new requests again or with sleep which I do like this now:
except Exception as e:
logger.error(e)
randomtime = random.randint(1,5)
logger.warn('ERROR - Retrying again website %s, retrying in %d secs' % (url, randomtime))
time.sleep(randomtime)
continue
and what I want to do is that whenever this error comes again as ERROR - I just want to basically 'skip' it meaning that it shouldn't give me a print of it but if there comes another error that is NOT
('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))
then it should print the error.
What do I need to do to make it print other errors but just continue with the Connection aborted without printing it?
Upvotes: 2
Views: 5769
Reputation: 3118
You should except RemoteDisconected separately from other exceptions:
from http.client import RemoteDisconnected
try:
...
except RemoteDisconnected:
continue
except Exception as e:
logger.error(e)
randomtime = random.randint(1,5)
logger.warn('ERROR - Retrying again website %s, retrying in %d secs' % (url, randomtime))
time.sleep(randomtime)
continue
Upvotes: 2
Reputation: 518
You can catch the RemoteDisconnected exception with:
try:
#your code here
except requests.exceptions.ConnectionError as e:
pass
except Exception as e:
logger.error(e)
randomtime = random.randint(1,5)
logger.warn('ERROR - Retrying again website %s, retrying in %d secs' % (url, randomtime))
time.sleep(randomtime)
continue
Although be careful about catching exceptions quietly, it may cause you problems later by stopping you identify the true cause of an issue.
Upvotes: 4
Reputation: 23538
You may try this:
if str(e) != 'Connection aborted.' :
logger.error(e)
However, connection can be aborted because of many different reasons, and you may want to add additional checks into the if
statement, checking e.reason
or other available fields.
Upvotes: 3