import socket import subprocess import sys #函数定义 def port_scan(ip,dest_port): try: for port in range(1,dest_port): sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) //创建套接字 re = sock.connect_ex((ip,port)) //探测 if re == 0: //判断 print(f'openportis {port}') sock.close() except KeyboardInterrupt: print("You pressed CTRL+C") sys.exit() except socket.error: print("Couldnt connect to server") sys.exit() port_scan("10.0.0.1",65535) //传参
输出结果如下: