From 1ec878378b6842957b67a2f3e665f19af7915c32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Sat, 20 Mar 2021 19:37:17 +0100 Subject: [PATCH] #7 Fixing false negative results --- ServerPingService.py | 14 +++++++++----- WebsitePingService.py | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/ServerPingService.py b/ServerPingService.py index 10cd54c..333cb06 100644 --- a/ServerPingService.py +++ b/ServerPingService.py @@ -7,8 +7,12 @@ def ping(host: str) -> int: :param host: :return: """ - res = ping3.ping(dest_addr=host, unit='ms') - if res: - return int(res) - else: - return -1 + NUMBER_OF_ATTEMPTS = 5 + + for iteration in range(NUMBER_OF_ATTEMPTS): + res = ping3.ping(dest_addr=host, unit='ms') + + if res: + return int(res) + + return -1 diff --git a/WebsitePingService.py b/WebsitePingService.py index 21ac161..f21bd59 100644 --- a/WebsitePingService.py +++ b/WebsitePingService.py @@ -6,12 +6,17 @@ def ping(url: str) -> int: :param url: The URL of the website to ping :return: The HTTP status code """ - try: - res = requests.get(url) - except: - return -1 - if res: - return res.status_code - else: - return -1 \ No newline at end of file + NUMBER_OF_ATTEMPTS = 5 + + for iteration in range(NUMBER_OF_ATTEMPTS): + res = None + try: + res = requests.get(url) + except: + pass + + if res: + return res.status_code + + return -1