-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetcheck.py
54 lines (42 loc) · 1.25 KB
/
netcheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#! /usr/bin/python
import time, urllib
class ConnectionStatusChecker:
# CONSTANTS
__INTERNET_CHECK_URL = 'http://google.com/robots.txt'
__SECONDS_BETWEEN_CHECKS = 30
# VARIABLES
__connectedState = True
__observer = None
def __init__(self, observer):
self.__observer = observer
def __checkConnection(self):
try:
# Check the connection by opening a socket to a URL
connection = urllib.urlopen(self.__INTERNET_CHECK_URL)
# Connected successfully so close the socket
connection.close()
self.__connected()
except Exception:
# We are not connected
self.__notConnected()
def __connected(self):
if (self.__connectedState == False):
self.__connectedState = True
self.__notifyObservers()
def __notConnected(self):
if (self.__connectedState == True):
self.__connectedState = False
self.__notifyObservers()
def __notifyObservers(self):
try:
self.__observer.statusChanged(self.__connectedState)
except Exception:
# Ignore all errors from observers
pass
def run(self):
while (True):
self.__checkConnection()
time.sleep(self.__SECONDS_BETWEEN_CHECKS)
class ConnectionStatusObserver:
def statusChanged(self, connected):
pass