-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_open_port
49 lines (42 loc) · 1.35 KB
/
test_open_port
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
import socket
import time
# Initial values to use
retry = 3
delay = 5
timeout = 3
def isOpen(ip, port, timeout):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((ip, int(port)))
s.shutdown(socket.SHUT_RDWR)
return True
except:
return False
finally:
s.close()
def checkHost(self, root, ip, port, retry, delay, timeout):
ipup = False
for i in range(retry):
if isOpen(ip, port, timeout):
ipup = True
break
else:
time.sleep(delay)
if ipup:
msg = f"{ip} is UP"
root.mainframe.ResultFrame.result.set(msg)
if len(msg) > 26:
# Resize for large URLs
root.mainframe.ResultFrame.label.configure(foreground="#389319", font=("segoe UI", 12))
else:
root.mainframe.ResultFrame.label.configure(foreground="#389319", font=("segoe UI", 14))
else:
msg = f"{ip} does NOT appear up"
root.mainframe.ResultFrame.result.set(msg)
if len(msg) > 26:
# Resize for large URLs
root.mainframe.ResultFrame.label.configure(foreground="#d80020", font=("segoe UI", 10))
else:
root.mainframe.ResultFrame.label.configure(foreground="#d80020", font=("segoe UI", 14))
self.button["state"] = "enabled"