def tcpsocket(hostname, wana=None, wanb=None, timeout=1): n =…
- User
def tcp_socket(hostname, wan_a=None, wan_b=None, timeout=1): n = QTime.currentTime().toString("hh:mm:ss") inspect_status = "" try: # First Checking with socket.create_connection((hostname, 80), timeout=timeout): inspect_status = "Server up" #if hostname.startswith("169"): print(f"{n} Ping PTH15067") return True, inspect_status except (socket.timeout, ConnectionRefusedError, OSError): # Second Checking if hostname.startswith(("PTH2", "PTH3")): try: ip = socket.gethostbyname(hostname) wan_ip = '.'.join(ip.split('.')[:-1] + ['10']) try: with socket.create_connection((wan_ip, 23), timeout=timeout): inspect_status = "Server down" return False, inspect_status except (socket.timeout, ConnectionRefusedError, OSError): inspect_status = "WAN down" return False, inspect_status except (socket.gaierror, socket.timeout, ConnectionRefusedError, OSError): inspect_status = "WAN down" return False, inspect_status elif hostname.startswith(("PTH11", "PTH15")) or hostname.startswith(("169" ,"s")): for ip, port in [(wan_a, 23), (wan_b, 23)]: if ip: try: with socket.create_connection((ip, port), timeout=timeout): inspect_status = "Server down" return False, inspect_status except (socket.timeout, ConnectionRefusedError, OSError): pass inspect_status = "WAN down" return False, inspect_status
if i don't know port at first check process
- Assistant