基于python3制作,脚本如下:
# This script runs on Python 3import socket, threading def TCP_connect(ip, port_number, delay, output): TCPsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) TCPsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) TCPsock.settimeout(delay) try: TCPsock.connect((ip, port_number)) output[port_number] = 'Listening' except: output[port_number] = '' def scan_ports(host_ip, delay): threads = [] # To run TCP_connect concurrently output = {} # For printing purposes # Spawning threads to scan ports for i in range(10000): t = threading.Thread(target=TCP_connect, args=(host_ip, i, delay, output)) threads.append(t) # Starting threads for i in range(10000): threads[i].start() # Locking the script until all threads complete for i in range(10000): threads[i].join() # Printing listening ports from small to large for i in range(10000): if output[i] == 'Listening': print(str(i) + ': ' + output[i]) def main(): host_ip = input("Enter host IP: ") delay = int(input("How many seconds the socket is going to wait until timeout: ")) scan_ports(host_ip, delay) input("Press Any Key") if __name__ == "__main__": main()
测试:
脚本如果直接发送xx.py文件给别人使用,可能还需要安装解释器,以及用的第三方包,不能直接使用,这里小编给打包成exe格式的,分享即可用。
首先安装pyinstaller,使用安装命令:pip install pyinstaller,这个不多说了。
我们来将这个.py的文件打包成一个exe,我们直接cmd切换到这个脚本的目录,执行命令:pyinstaller-F scan port.py,如下图所示。
ps: -F参数表示覆盖打包,这样在打包时,不管我们打包几次,都是最新的,这个记住就行,固定命令。
2、执行完毕之后,会生成几个文件夹,如下图所示。
3、在dist里面呢,就有了一个exe程序,这个就是可执行的exe程序,如下图所示。
4、我们来运行一下这个,效果图如下图所示。