'''
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()