From 5aad799fd0f59742a4c296b4b4d4ea012147dce2 Mon Sep 17 00:00:00 2001 From: Ahmad Kataranjee <93165947+ajee10x@users.noreply.github.com> Date: Fri, 30 Aug 2024 01:33:00 +0200 Subject: [PATCH] Update CloudSniffer.py Improve the error handling by using more specific exception handling in check_url function --- CloudSniffer.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/CloudSniffer.py b/CloudSniffer.py index 916b6da..2607c53 100644 --- a/CloudSniffer.py +++ b/CloudSniffer.py @@ -88,13 +88,21 @@ def check_url(ip, url): print(Fore.YELLOW + f'IP {ip} caused a redirect, might be real.') if trace: trace_file.write(f'IP {ip} caused a redirect, might be real.\n') + except requests.exceptions.ConnectTimeout: + if trace: + trace_file.write(f'Request for IP {ip} failed with timeout error.\n') + except requests.exceptions.HTTPError as errh: + if trace: + trace_file.write(f'Http Error: {str(errh)} for IP {ip}\n') + except requests.exceptions.ConnectionError as errc: + if trace: + trace_file.write(f'Error Connecting: {str(errc)} for IP {ip}\n') + except requests.exceptions.Timeout as errt: + if trace: + trace_file.write(f'Timeout Error: {str(errt)} for IP {ip}\n') except requests.exceptions.RequestException as err: - if isinstance(err, requests.exceptions.ConnectTimeout): - if trace: - trace_file.write(f'Request for IP {ip} failed with timeout error: {err}\n') - else: - if trace: - trace_file.write(f'Request for IP {ip} failed with error: {err}\n') + if trace: + trace_file.write(f'Request Error: {str(err)} for IP {ip}\n') for ip in tqdm(ip_addresses, ncols=75, bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt}'): ip = ip.strip()