Update ohcapi.py

A more elegant code to test the internet connection

Signed-off-by: Frédéric <fmatray@users.noreply.github.com>
This commit is contained in:
Frédéric
2025-01-20 22:01:10 +01:00
committed by GitHub
parent 8d1a5babe8
commit 89af46c6fc

View File

@ -75,20 +75,16 @@ class ohcapi(plugins.Plugin):
return return
# Check if the internet is still available by pinging Google # Check if the internet is still available by pinging Google
self.internet_active = False
try: try:
response = requests.get('https://www.google.com', timeout=5) response = requests.get('https://www.google.com', timeout=5)
except requests.ConnectionError:
self.internet_active = False
return
if response.status_code == 200: if response.status_code == 200:
self.internet_active = True self.internet_active = True
else: except requests.ConnectionError:
self.internet_active = False
return return
current_time = time.time() current_time = time.time()
if current_time - self.last_run >= self.options['sleep']: if self.internet_active and current_time - self.last_run >= self.options['sleep']:
self._run_tasks(agent) self._run_tasks(agent)
self.last_run = current_time self.last_run = current_time