PHP前端开发

利用Python实现简易端口扫描器方法介绍

百变鹏仔 6小时前 #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] &gt; 8    ip[1] = (num &amp; 0xff0000) &gt;&gt; 16    ip[0] = (num &amp; 0xff000000) &gt;&gt; 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><span style="font-family: " microsoft yahei font-size:>使用效果如下:</span></p><p><img src="https://img.php.cn/upload/article/000/000/013/13e68be8b0558e13d96cf0ba87043123-2.png" alt="利用Python实现简易端口扫描器方法介绍"    style="max-width:90%"  style="max-width:90%" title="利用Python实现简易端口扫描器方法介绍"></p><p><img src="https://img.php.cn/upload/article/000/000/013/a7324f72e0ad3665219cb71139f6eabb-3.png" alt="利用Python实现简易端口扫描器方法介绍"    style="max-width:90%"  style="max-width:90%" title="利用Python实现简易端口扫描器方法介绍"></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p>