Loading... ``` # 检测端口是否可以连通 import socket import time def check_tcp_port(host, port, timeout=3): start_time = time.time() sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(timeout) try: sock.connect((host, port)) elapsed_time = time.time() - start_time return True, elapsed_time except socket.error as err: return False, 0 finally: sock.close() ### body+="\r\n" print("nls端口连通性探测:") body+="nls端口连通性探测:" body+="\r\n" # 要检查的主机和端口 host = 'www.baidu.com' port = 80 # 检查端口 is_open, latency = check_tcp_port(host, port) if is_open: print(f"Port {port} on {host} is open. Latency: {latency:.3f} seconds") body+=f"Port {port} on {host} is open. Latency: {latency:.3f} seconds" body += "\r\n" else: print(f"Port {port} on {host} is not open or the host is unreachable.") body+=f"Port {port} on {host} is not open or the host is unreachable." body += "\r\n" print() ``` 最后修改:2024 年 05 月 11 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏