Python IP子网测试教学游戏

'''
IpSubClassTestGame by 郑瑞国
'''
import random,time,ipaddress

def genIP():
    ip = ipaddress.IPv4Address(
        str(random.randint(1,255))+'.'+
        str(random.randint(1,255))+'.'+
        str(random.randint(1,255))+'.'+
        str(random.randint(1,255)))
    if (ipaddress.IPv4Address('10.0.0.0')<=ip<=ipaddress.IPv4Address('126.0.0.0') or
        ipaddress.IPv4Address('128.0.0.0')<=ip<=ipaddress.IPv4Address('191.255.0.0') or
        ipaddress.IPv4Address('192.0.0.0')<=ip<=ipaddress.IPv4Address('223.255.255.0') ):
        return(ip)
    else:
        genIP()
def binIP(ip):
    a,b,c,d = str(ip).split('.')
    list = [bin(int(a))[2:],bin(int(b))[2:],bin(int(c))[2:],bin(int(d))[2:]]
    L = '.'.join(list)
    return(L)    
def testIP(ip):
    if (ip.is_private):
            print(ip,'私有IP:',ip.is_private,L)
def subClassIP(ip):
    '''IP分类'''
    if(ipaddress.IPv4Address('10.0.0.0')<=ip<=ipaddress.IPv4Address('126.0.0.0')):
        #print ("A类 IP")
        flag='A'
    elif(ipaddress.IPv4Address('128.0.0.0')<=ip<=ipaddress.IPv4Address('191.255.0.0')):
        #print ("B类IP")
        flag='B'
    elif(ipaddress.IPv4Address('192.0.0.0')<=ip<=ipaddress.IPv4Address('223.255.255.0')):
        #print ("C类 IP")
        flag='C'
    else:
        flag='没测出来'
    return(flag)
def displayIP():
    ip = genIP()
    if ip:
        print('\n')
        print(ip)
        MyBinIPinput = input('请输入此IP地址的二进制形式\n如(11011011.11101111.11110111.11101101):\n')
        MyClassIPinput = input('请输入此IP的类别(如:A、B、C):')
        print(binIP(ip),'\t',ip,'\t',subClassIP(ip))
        if MyBinIPinput == binIP(ip):
            print('您输入的地址正确')
        else:
            print('您输入的地址错误')
        if MyClassIPinput == subClassIP(ip):
            print('您输入的类型正确')
        else:
            print('您输入的类型错误')            
def main():
        for i in range(1,10):
            ip =ipaddress.IPv4Address(input("请输入IP:"))
            print(subClassIP(ip))
    
if __name__ == "__main__":
    main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云空

人工智能,深度学习,神经网络

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值