Update on Networking Code

This is the code that I just finished, this is basically improved with the Binary Representation stuff and also with a use of Decorators to provide customizations for Invalid IPs, apart from this I don't think that I can do anything much from just the Classful Addresses because I need atleast the Subnet Masks for providing any other information related to the Address like the Hosts or the No. of Networks or the no. of Subnets possible for the Address. 

The things become so damn easy with Python for such scenarios where Python is just near to perfect to use because of the excellent syntactic sugar and the lack of Complexity, if I remove the Decorator of course....it's 12:30 midnight now, and I am left with a problem Statement for the Day, as there was a function that I had to attend today so things were stacked, tomorrow I am thinking of doing some writing work for the Balderdash assignments of my college, literally if they did a single thing that would make sense then it would be more than a miracle, But hoping for this is just like staring into a deep abyss, there's no chance this would happen in my opinion. 

Well, this is the code:-

# Helper Function for the classless Func
def getBinary(ipaddress):
    # Binary Representation of the IP Address
    print("Binary Representation: ",end="")
    for ind,octet in enumerate(ipaddress.split('.')):
        print(f"{bin(int(octet))[2:]}",end="." if(ind!=3) else "\n")

       
def decor(func):
    def innerFunc(ipaddress):
        result=func(ipaddress)
        if(result==None):
            return "This was an Invalid IP, no Classless Address available!"
        else:
            return result
    return innerFunc
@decor
def classless(ipaddress):
    '''
        Will specify the Class of the IP Address and will return the Classless IP
        Address
    '''
    # Alongwith the info of the Default Subnet Mask, info like no. of bits used for NID and no. of bits used for HID can also be specified as they depend on the Class only
    # The no. of Hosts can also be specified on the basis of the Input IP Address
    print(ipaddress)
    firstOctet=int(ipaddress[:ipaddress.index('.')])
    if(1<=firstOctet<=126):
        print("Class A!")
        print("Default Subnet Mask: 255.0.0.0")
        getBinary(ipaddress)
        return ipaddress+'/8'
    elif(128<=firstOctet<=191):
        print("Class B!")
        print("Defaul Subnet Mask: 255.255.0.0")
        getBinary(ipaddress)
        return ipaddress+'/16'
    elif(192<=firstOctet<=223):
        print("Class C!")
        print("Default Subnet Mask: 255.255.255.0")
        getBinary(ipaddress)
        return ipaddress+'/24'
    else:
        print("This is a reserved IP Address, INVALID!!")
        getBinary(ipaddress)
   
       
print(classless("192.168.0.0"))
print()
print(classless("172.16.0.0"))
print()
# print(classless("224.0.0.0") if(classless("224.0.0.0")!=None) else "There was nothing returned from the Function!")
print(classless("224.0.0.0"))

Comments

Popular Posts