forked from jadijadi/gittutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternet.py
More file actions
executable file
·56 lines (47 loc) · 1.62 KB
/
internet.py
File metadata and controls
executable file
·56 lines (47 loc) · 1.62 KB
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
import socket
import time
import os
"""
This functions checks connection to Google DNS server
If DNS server is reachable on port 53, then it means that
the internet is up and running
"""
#Initial Marks for Connectivity or not
check = "\N{Heavy Check Mark}"
fail = "\N{Heavy Multiplication X}"
def internet_connected(host="8.8.8.8", port=53):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(1)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
pass
return False
try:
# Store value of last state in this variable
counter = 0
while True:
if internet_connected():
# find out what is the os
uname = os.uname()
# Clear Command line Session
if uname.sysname == 'Linux' or uname.sysname == 'Darwin':
os.system('clear')
print(f"You are on a {uname.sysname} machine")
elif uname.sysname == 'Windows':
os.system('cls')
print("You are on a Windows machine")
counter += 1
print(f"Internet is up {check}\n{counter} sec \n{round(counter / 60, 2)} min \n{round(counter / 3600, 2)} hour\n")
# Wait for 1 second before checking for internet connectivity
time.sleep(1)
else:
# If previously internet connected, then print message
print(f"Internet is down ... {fail}")
except KeyboardInterrupt:
print("Exiting... Bye!")