-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-comms-socket-1.py
62 lines (54 loc) · 1.37 KB
/
io-comms-socket-1.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
58
59
60
61
62
# Import libraries
import socket
import time
# Setup all working variables
port = 443
retry = 5
delay = 1
timeout = 3
# List websites to test
ip = ["facebook.com",
"google.com",
"fbi.gov",
"nottingham.ac.uk",
"digiscore.github.io",
"garbage.gigo"
]
# Function that opens an ip Socket
def isOpen(ip, port):
# Instantiate a socket object for a given ip address
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
# Try to connect and if successful return a True bool
try:
s.connect((ip, int(port)))
s.shutdown(socket.SHUT_RDWR)
return True
except:
return False
# Important to close it
finally:
s.close()
# Function that dispatches socket info to isOpen func and returns results
def checkHost(ip, port):
# Sets condition to False
is_up = False
# For a number of trys
for t in range(retry):
print(f"Checking {ip}")
if isOpen(ip, port):
is_up = True
break
else:
time.sleep(delay)
return is_up
# Function controlling the main loop
def main():
# For each web address in ip list check if it is up and running
for i in ip:
if checkHost(i, port):
print(f"\t\t{i} is UP and running")
time.sleep(1)
# Code starts here
if __name__ == "__main__":
main()