基于Python的ip地址计算

  本文分享了基于Python的ip地址计算程序,取需。如有错误,欢迎批评指正,谢谢。


  【代码】

# -*- coding: utf-8 -*-
"""
Created on Mon Jan 25 22:35:50 2021

@author: xyyang
"""
import re

class IP:
    def __init__(self,*ip):
        if len(ip)==1:
            self.ip=ip[0] #ip
            if self.GetType()=="A":
                self.subnetmask="255.0.0.0" #标准子网掩码
            elif self.GetType()=="B":
                self.subnetmask="255.255.0.0"
            elif self.GetType()=="C":
                self.subnetmask="255.255.255.0"
            self.netWorkNumber=self.CalnetWorkNumber()
            self.hostNumber=self.CalhostNumber()
        elif len(ip)>=2:
            self.ip=ip[0]
            self.subnetmask=ip[1]
            self.netWorkNumber=self.CalnetWorkNumber()
            self.hostNumber=self.CalhostNumber()
    
    def GetType(self):
        if re.search('[2-9]',self.ip)==None:
            self.bin2dec('ip')
        firstbyte=int(self.ip.split('.')[0])
        if 1<=firstbyte<=126:#1.0.0.0~126.255.255.255
            return "A"
        elif firstbyte==127:#环回地址(127.0.0.1)
            return "Loopback address"
        elif 128<=firstbyte<=191:#128.0.0.0~191.255.255.255
            return "B"
        elif 192<=firstbyte<=223:#192.0.0.0~223.255.255.255
            return "C"
        elif 224<=firstbyte<=239:#多点广播地址(224.0.0.0~239.255.255.255)
            return "D"
        elif 240<=firstbyte<=247:#尚未使用(240.0.0.0~247.255.255.255)
            return "E"
        elif self.ip=="255.255.255.255":#有限广播地址
            return "Limited broadcast address"
    
    def GetsubnetMask_std(self):
        return self.subnetmask
    
    def bin2dec(self,tip):
        if tip=="ip":
            if re.search('[2-9]',self.ip)!=None:
                print("This is already a decimal ip1!")
                return
            ips=self.ip.split('.')
            self.ip=""
            for item in ips:
                self.ip+=str(int(item,2))+'.'
            self.ip=self.ip[:-1]
        elif tip=="mask":
            if re.search('[2-9]',self.subnetmask)!=None:
                print("This is already a decimal ip2!")
                return
            masks=self.subnetmask.split('.')
            self.subnetmask=""
            for item in masks:
                self.subnetmask+=str(int(item,2))+'.'
            self.subnetmask=self.subnetmask[:-1]
        elif tip=="nwn":
            if re.search('[2-9]',self.netWorkNumber)!=None:
                print("This is already a decimal ip3!")
                return
            nwns=self.netWorkNumber.split('.')
            self.netWorkNumber=""
            for item in nwns:
                self.netWorkNumber+=str(int(item,2))+'.'
            self.netWorkNumber=self.netWorkNumber[:-1]
    
    def dec2bin(self,tip):
        if tip=="ip":
            if re.search('[2-9]',self.ip)==None:
                print("This is already a binary ip4!")
                return
            ips=self.ip.split('.')
            self.ip=""
            for item in ips:
                self.ip+=(bin(int(item))[2:]).zfill(8)+'.'
            self.ip=self.ip[:-1]
        elif tip=="mask":
            if re.search('[2-9]',self.subnetmask)==None:
                print("This is already a binary ip5!")
                return
            masks=self.subnetmask.split('.')
            self.subnetmask=""
            for item in masks:
                self.subnetmask+=(bin(int(item))[2:]).zfill(8)+'.'
            self.subnetmask=self.subnetmask[:-1]
        elif tip=="nwn":
            if re.search('[2-9]',self.netWorkNumber)==None:
                print("This is already a binary ip6!")
                return
            nwns=self.netWorkNumber.split('.')
            self.netWorkNumber=""
            for item in nwns:
                self.netWorkNumber+=(bin(int(item))[2:]).zfill(8)+'.'
            self.netWorkNumber=self.netWorkNumber[:-1]
    
    def CalnetWorkNumber(self):
        self.dec2bin('ip')
        self.dec2bin('mask')
        if len(self.ip)!=len(self.subnetmask):
            print("These two ips can't do and operation!")
            return
        netWorkNumber=""
        for i in range(len(self.ip)):
            if self.ip[i]=='.':
                netWorkNumber+='.'
            else:
                if self.ip[i]=='1' and self.subnetmask[i]=='1':
                    netWorkNumber+='1'
                else:
                    netWorkNumber+='0'
        return netWorkNumber #binary
    
    def CalhostNumber(self):
        self.dec2bin('ip')
        self.dec2bin('mask')
        maskOpp=""
        for item in self.subnetmask:
            if item=='.':
                maskOpp+='.'
            else:
                if item=='1':
                    maskOpp+='0'
                else:
                    maskOpp+='1'
        hostNumber=""
        for i in range(len(self.ip)):
            if self.ip[i]=='.':
                hostNumber+='.'
            else:
                if self.ip[i]=='1' and maskOpp[i]=='1':
                    hostNumber+='1'
                else:
                    hostNumber+='0'
        return hostNumber
    
    @staticmethod
    def BIN2DEC(bip):
        bips=bip.split('.')
        dip=""
        for item in bips:
            dip+=str(int(item,2))+'.'
        return dip[:-1]
    
    @staticmethod
    def DEC2BIN(dip):
        dips=dip.split('.')
        bip=""
        for item in dips:
            bip+=(bin(int(item))[2:]).zfill(8)+'.'
        return bip[:-1]

if __name__=="__main__":
    a=IP("192.9.200.13")
    print(a.GetType())

你可能感兴趣的:(Python,Learning,小知识积累,ip,子网掩码,网络号,主机号,Python)