I have written a small Python function to verify if a string is a valid IPv4 subnet mask.
It works, but maybe it's too complicated and smooth? Or maybe it could be improved / optimized / rewritten in a smarter way than just to mark 1 in a chain? ..
IsIPv4Address is another one of my functions (EDIT 1: added to the question).
Specifically I do not want to use any library / external functions since this is more of a study in Python 🙂
def IsIPv4Address (ipAdd): "" "Validate an IPv4 address" "" octets = ipAdd.split (".") yes len (bytes)! = 4: false return for octets in octets: If it is not IsInteger (octet): false return if int (octet)> 255 or int (octet) <0: false return return true def IsIPv4Mask (ipMask): "" "Validate an IPv4 subnet mask" "" # Each mask looks like an IPv4 address and must pass the checks if it is not IsIPv4Address (ipMask): false return ipMaskBinary = "" ipMaskBinary = ipMaskBinary.join ([bin(int(oct))[2:] for oct in ipMask.split (".")]) isBitZero = ipMask == "0" for bit in ipMaskBinary[1:]: if bit == "1" and isBitZero: false return if bit == "0": isBitZero = True return true