python编写Ipaddress类

编写Ipaddress类,要求:
(1)自定义私有和公有属性,以完成后续的功能为依据。
(2)具有以下方法:①得到类别、②得到子网掩码、③得到网络地址(子网地址)、④得到网络广播地址(子网广播的地址)、⑤判断与别的IP地址是否在一个网段(输入可以是一个Ipaddress类,也可以是一个带前缀的IP地址字符串,如果格式不正确给出错误提示)、⑥列出同网段内的可分配主机的所有Ip地址、⑦显示基本信息。

(输入时为三个,第一个是一个或多个ipv4字符串,第二个和第三个分别为两个要比较的ip地址)

class Ipaddress:
    def __init__(self,string,string1,string2):#初始化三个数据,输入时必须是三个否则报错
        self.string=string
        self._string1=string1
        self._string2=string2
        
    def getstring1(self):
        return self._string1 
    def setstring1(self,value):
        self._string1=value
    def setstring2(self,value):
        self._string2=value
    def getstring2(self):
        return self._string2
    string1=property(getstring1,setstring1)
    string2=property(getstring2,setstring2)
    
    def true_ipv4(string):      #IP地址的格式是否正确
        ipv4=[]
        ipv4=string.split("/")
        temp=[]
        temp=ipv4[0].split(".")
        if string.count("/")==1:
            try:
                for length in range(0,len(temp)):
                    if 0<=int(temp[length])<256 and 0<=int(ipv4[1])<=32:
                        if length==3:
                            return True
            except:
                return False
        else:
            return False
        
    def ABCDE_ipv4(string):      #IP地址的种类
        temp=[]
        temp=string.split(".")
        if 0<=int(temp[0])<127:
            print("A类地址")
        elif 128<=int(temp[0])<192:
            print("B类地址")
        elif 192<=int(temp[0])<224:
            print("C类地址")
        elif 224<=int(temp[0])<240:
            print("D类地址")
        elif 240<=int(temp[0]):
            print("E类地址")
        else:
            print("其它类地址")
            
    def network_ipv4(string):   #网络地址,网络广播地址 1-31
        ipv4=[]
        ipv4=string.split("/")
        front=""            #网络地址
        behind=""           #网络广播地址
        mac=""
        temp=int(ipv4[1])//8         #固定位
        work=ipv4[0].split(".")
        flag=8-(int(ipv4[1])%8)      #子网IP数
        if int(ipv4[1])==32:
            return ipv4[0]+" "+ipv4[0]+" "+"255.255.255.255"
        for length in range(0,temp):    #获取固定前缀
            front=front+work[length]+"."
            mac=mac+"255"+"."
        behind=front
        mac=mac+str(256-2**flag)
               #子网网段的判定
        for length in range(0,2**(8-flag)):
            if (2**flag)*length<=int(work[temp])<(2**flag)*(length+1):
                front=front+str((2**flag)*length)
                behind=behind+str((2**flag)*(length+1)-1)
        for length in range((temp+1),4):
            front=front+".0"
            mac=mac+".0"
            behind=behind+".255"
        return front+" "+behind+" "+mac

    def different_or_same_ipv4(self):       #判断两个IP地址是否在一个网段
        temp=[]
        word=""
        if Ipaddress.true_ipv4(self._string1)==True:
            if Ipaddress.true_ipv4(self._string2)==True:
                if Ipaddress.network_ipv4(self._string1)==Ipaddress.network_ipv4(self._string2):
                    return True
                else:
                    temp=Ipaddress.network_ipv4(self._string1).split(" ")
                    word=word+temp[0]+" "
                    temp=Ipaddress.network_ipv4(self._string2).split(" ")
                    word=word+temp[0]
                    return word
            else:
                print("比较的第二个地址错误!")
        else:
            print("比较的第一个地址错误!")
            
    def is_network_ipv4(string):        #属于同网段内的所有Ip地址(返回 key网络,广播 value Ip地址)
        temps=[]    #将一串ipv4分割
        temp=[]     #将ip和前缀分割
        front=[]    #得到netwok_ipv4 的 网络 广播 掩码
        work={}     #同网段内的的所有Ip地址
        temps=string.split(" ")
        for length in temps:
            temp=length.split("/")
            front=Ipaddress.network_ipv4(length).split(" ")
            value=work.get(str(front[0]+" "+front[2]),None) #判断原字典中是否有该key,没有返回None
            if value==None:
                work[str(front[0]+" "+front[2])]=temp[0]
            else:
                if temp[0] in value:        #判断中该key是否有value
                    pass
                else:
                    work[str(front[0]+" "+front[2])]=value+" "+temp[0]
        return work
    
    def work(self):
        print("\t注意:\n\t   (1)输入第一个字符串的ipv4地址带有前缀(可以是多个ip)\n\t   且各个地址之间用一个空格隔开")
        print("\t   (2)输入第二个和第三个的只是一个ipv4地址(带有前缀)\n\t   进行比较\n")
        temps=self.string.split(" ") #将一串ipv4分割
        temp=""                      #分割时的临时字符串   
        count=0                      #把分割地址标号,地址错误时,返回它的值
        true_string=[]               #正确的地址
        for length in temps:
            count+=1
            if Ipaddress.true_ipv4(length)==True:
                temp=temp+length+" "
            else:
                print("输入字符串中第",count,"个ipv4地址错误!")
        flag=temp.split(" ")        #分割时的临时列表
        true_string=flag[0:-1]
        #print(true_string)
        while 1:
            print("\n\t以下选项对应相应功能:")
            print("\t1:得到类别\n\t2:得到子网掩码\n\t3:得到网络地址\n\t4:得到网络广播地址")
            print("\t5:判断是否在一个网段\n\t6:列出同网段内的主机的所有Ip地址\n\t7:显示基本信息\n\t0或其它:退出")
            choice=str(input("\n\t请输入你的选项:"))		#choice 功能选择
            if choice=="1":
                for length in true_string:
                    print(length)
                    Ipaddress.ABCDE_ipv4(length)
            elif choice=="2":
                for length in true_string:
                    flag=Ipaddress.network_ipv4(length).split(" ")#此时flag接收netwok_ipv4的网络 广播 掩码
                    print(length)
                    print(flag[2],"\n")
            elif choice=="3":
                for length in true_string:
                    flag=Ipaddress.network_ipv4(length).split(" ")#此时flag接收netwok_ipv4的网络 广播 掩码
                    print(length)
                    print(flag[0],"\n")
            elif choice=="4":
                for length in true_string:
                    flag=Ipaddress.network_ipv4(length).split(" ")#此时flag接收netwok_ipv4的网络 广播 掩码
                    print(length)
                    print(flag[1],"\n")
            elif choice=="5":
                print(Ipaddress.different_or_same_ipv4(self))
            elif choice=="6":
                temp=""
                count=0
                for length in true_string:
                    count+=1
                    if count==len(true_string):
                        temp=temp+length
                    else:
                        temp=temp+length+" "
                print(Ipaddress.is_network_ipv4(temp))
            elif choice=="7":
                for length in true_string:
                    print(length)
                    Ipaddress.ABCDE_ipv4(length)
                    print(Ipaddress.network_ipv4(length),"\n")
            else:
                break
        

if __name__=="__main__":    						#检测代码
    p1=Ipaddress("192.1.1.1/32 192.168.1.2/3 1168.1.1/","","")
    p1.string1="192.1.1.13/2"
    p1.string2="192.1.1.1/22"
    p1.work()
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值