加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

匹配IP和匹配域名

发布时间:2020-05-24 23:16:16 所属栏目:Python 来源:互联网
导读:匹配IP和匹配域名

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

class JianKong():
    '''查询IDC信息,封ip和过白名单'''
    def __init__(self):
        pass
    @classmethod
    def ip_verify(cls,str):
        '验证IP地址规范'
        pattern=re.compile('(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]).(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])')
        s=pattern.findall(str)
        if len(s)>0:
            ip=s[0][0]+'.'+s[0][1]+'.'+s[0][2]+'.'+s[0][3]
            return ip
        else:
            print 'IP格式不正确'#弹窗提醒
            return ''
    @classmethod
    def domain_verify(cls,domainStr):
        '验证域名规范,返回合法域名列表'
        domainList=[]
        file=open('c:domain.txt','r')
        domainType=file.readlines()
        #去重
        domainType=list(set(domainType))
        #print domainType
        file.close()
        #file=['com','ac','com.cn','net'+'']
        for line in domainType:
            #文件中动态读取每个顶级域名进行匹配
            line=line.strip()
            pattern=re.compile('([a-z0-9][a-z0-9-]*?.'+line+')(?:s|$)+',re.S)
            #例如[a-z0-9][a-z0-9-]*?.com.cn(?:s|$)+ 中(?:s|$)表示域名后缀后面必须是空白符或者字符结束(?:)表示括号不用于分组功能
            #防止.com.cn先匹配到.com即停止匹配导致错误,或者匹配到.comc多了字符
            result=pattern.findall(domainStr)
            if len(result)>0:
                #正确结果添加到返回列表
                domainList=domainList+result
        #去重
        domainList=list(set(domainList))
        newList=[]
        for d in domainList:
            if d not in domainType and d+'n' not in domainType:
                newList.append(d)
                    
        return newList
    @classmethod
    def getDomainType(cls):
        '从工信部网站获取所有合法域名后缀'
        file=open('c:/domain.txt','w')
        p=re.compile('class="by2">.(.*?)&nbsp;</td>',re.S)
        for i in range(1,23):
            data='domainName=&domainBlur=0&page.pageSize=20&pageNo='+str(i)+'&jumpPageNo='+str(i)
            header={'Host':'www.miitbeian.gov.cn','Origin':'http://www.miitbeian.gov.cn','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/44.0.2403.157 UBrowser/5.5.6125.14 Safari/537.36','Referer':'http://www.miitbeian.gov.cn/basecode/query/queryDomain.action;jsessionid=HSTRWpLZbR0cX4vFkDpnpbNBYyRl4GwW1fxpyhdyc0fcfhkvJTBV!1139295987'}
            url='http://www.miitbeian.gov.cn/basecode/query/queryDomain.action;jsessionid=HSTRWpLZbR0cX4vFkDpnpbNBYyRl4GwW1fxpyhdyc0fcfhkvJTBV!1139295987'
            request=urllib2.Request(url,data,header)
            response=urllib2.urlopen(request)
            recv=response.read()
            s=p.findall(recv)
            #print s
            #去重
            s=list(set(s))
            for y in s:
                file.write(y+'n')
                file.flush()
                #print str(i)+' '+y
        file.close()
        print '完毕'#弹窗完成

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读