BW3-Core/boswatch/network/netCheck.py

49 lines
1.6 KiB
Python
Raw Permalink Normal View History

2018-09-21 12:35:23 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
r"""!
2018-09-21 12:35:23 +02:00
____ ____ ______ __ __ __ _____
/ __ )/ __ \/ ___/ | / /___ _/ /______/ /_ |__ /
/ __ / / / /\__ \| | /| / / __ `/ __/ ___/ __ \ /_ <
/ /_/ / /_/ /___/ /| |/ |/ / /_/ / /_/ /__/ / / / ___/ /
/_____/\____//____/ |__/|__/\__,_/\__/\___/_/ /_/ /____/
German BOS Information Script
by Bastian Schroll
@file: netCheck.py
@date: 21.09.2018
@author: Bastian Schroll
@description: Worker to check internet connection
"""
import logging
from urllib.request import urlopen
logging.debug("- %s loaded", __name__)
class NetCheck:
r"""!Worker class to check internet connection"""
2018-09-21 12:35:23 +02:00
def __init__(self, hostname="https://www.google.com/", timeout=1):
r"""!Create a new NetCheck instance
2018-09-21 12:39:36 +02:00
@param hostname: host against connection check is running ("https://www.google.com/")
2019-10-26 16:23:42 +02:00
@param timeout: timeout for connection check in sec. (1)"""
2018-09-21 12:35:23 +02:00
self._hostname = hostname
self._timeout = timeout
2019-10-25 13:43:50 +02:00
self.connectionState = False
2018-09-23 18:44:58 +02:00
self.checkConn() # initiate a first check
2018-09-21 12:35:23 +02:00
def checkConn(self):
r"""!Check the connection
2018-09-21 12:39:36 +02:00
@return True or False"""
2018-09-21 12:35:23 +02:00
try:
urlopen(self._hostname, timeout=self._timeout)
2018-09-21 15:27:02 +02:00
logging.debug("%s is reachable", self._hostname)
2019-10-25 13:43:50 +02:00
self.connectionState = True
2018-09-21 12:35:23 +02:00
return True
2019-03-02 09:15:40 +01:00
except: # todo find right exception type
2018-09-21 15:27:02 +02:00
logging.warning("%s is not reachable", self._hostname)
2019-10-25 13:43:50 +02:00
self.connectionState = False
2018-09-21 12:35:23 +02:00
return False