实现端口扫描器Python代码实例
本篇文章主要介绍了python实现简易端口扫描器的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
在网上的一些资料的基础上自己又添了些新内容,算是Python socket编程练手吧。
#coding=utf-8import socketimport timeimport sysimport structimport threadingfrom threading import Thread,activeCountresults=[]def portScanner(ip,port): server = (ip,port) sockfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sockfd.settimeout(0.1) #设置阻塞模式下socket的超时时间 ret = sockfd.connect_ex(server) #成功返回0,失败返回error的值。 if not ret: sockfd.close() results.append([ip,port]) #print '%s:%s is opened...' % (ip,port) else: sockfd.close() pass return '' def ip2num(ip): #将ip地址转换成数字 lp = [int(x) for x in ip.split('.')] return lp[0] > 8 ip[1] = (num & 0xff0000) >> 16 ip[0] = (num & 0xff000000) >> 24 return '%s.%s.%s.%s' % (ip[0],ip[1],ip[2],ip[3])def iprange(ip1,ip2): num1 =socket.ntohl(struct.unpack("I",socket.inet_aton(str(ip1)))[0]) num2 =socket.ntohl(struct.unpack("I",socket.inet_aton(str(ip2)))[0]) tmp = num2 - num1 if tmp <p>使用效果如下:</p><p><img src="https://img.php.cn/upload/article/000/001/505/c04caaab50420c2e2e0354a346951d4b-0.png" alt=""></p><p><img src="https://img.php.cn/upload/article/000/001/505/59da3095616729ef695c8326ee8fa83c-1.png" alt=""></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="max-width:90%" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><p>【