先上代码

启动这个程序用的格式为
python port_scan.py <host> <start_port>-<end_port>

接下来解释代码

用的sys.argv来获取传过来的参数。host得到第一个参数,portstrs得到第二个参数,其中<start_port>-<end_port>整体是第二个参数,所以需要分割成两个,然后再进行分配。
target_ip= gethostbyname(host)
用来获取目标的ip
sock= socket(AF_INET, SOCK_STREAM)
这个来创建TCP socket,可参见Python Socket 编程详细介绍

这个是加入了多线程。一个是加入了锁,开启新线程,用的是start_new_thread。调用tcp_test函数,传递参数(port,)。

还有一个Python端口扫描包python-nmap,不过我经常直接用nmap。
文章参考:
网友评论