-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternet_access.py
57 lines (42 loc) · 1.64 KB
/
internet_access.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
55
56
57
import threading
import urllib.request
import threading
import urllib.request
import time
class GetInternetAccess():
def __init__(self, time_out) -> None:
self.time_out = time_out
self.connection_status = False
def get_status(self):
def _is_connected():
try:
urllib.request.urlopen('https://google.com', timeout=self.time_out)
return True
except urllib.request.URLError:
return False
self.connection_status = _is_connected()
print("Internet status: ", self.connection_status)
return self.connection_status
class InternetAccess():
def __init__(self, time_out) -> None:
self.time_out = time_out
self.connection_status = False
self.mutex = threading.Lock()
def _start(self):
def _update_connection_status():
def _is_connected():
try:
urllib.request.urlopen('https://google.com', timeout=self.time_out)
return True
except urllib.request.URLError:
return False
while True:
with self.mutex:
self.connection_status = _is_connected()
print("Internet status: ", self.connection_status)
time.sleep(self.time_out)
threading.Thread(target=_update_connection_status, daemon=True).start()
def get_status(self):
with self.mutex:
connection_status_copy = self.connection_status
return connection_status_copy