Commit 93ba433b authored by Devaev Maxim's avatar Devaev Maxim

Fixed small bug in IPv4 address validator, added validator for IPv4 netmask…

Fixed small bug in IPv4 address validator, added validator for IPv4 netmask validator, validator for hardware MAC address
parent 38d2eca5
......@@ -32,18 +32,65 @@ def validStringList(arg) :
def validIpv4Address(arg) :
arg = str(arg).strip()
octets_list = []
for octets_list_item in arg.split(".") :
octets_list = arg.split(".")
if len(octets_list) != 4 :
raise ValidatorError("Argument \"%s\" is not valid IPv4 address" % (arg))
for count in xrange(4) :
try :
octets_list_item = int(octets_list_item)
if not 0 <= octets_list_item <= 256 :
octets_list[count] = int(octets_list[count])
if not 0 <= octets_list[count] <= 255 :
raise Exception
octets_list.append(octets_list_item)
except :
raise ValidatorError("Argument \"%s\" is not valid IPv4 address" % (arg))
if len(octets_list) != 4 :
raise ValidatorError("Argument \"%s\" is not valid IPv4 address" % (arg))
return (arg, octets_list)
def validIpv4Netmask(arg) :
arg = str(arg).strip()
octets_list = arg.split(".")
if len(octets_list) == 1 :
try :
arg = int(arg)
except :
raise ValidatorError("Argument \"%s\" is not valid IPv4 netmask" % (arg))
octets_list = [0] * 4
one_count = arg
for count in xrange(4) :
octet_one_count = 8
while one_count and octet_one_count :
octets_list[count] |= 128 >> 8 - octet_one_count
one_count -= 1
octet_one_count -= 1
elif len(octets_list) == 4 :
for count in xrange(4) :
try :
octets_list[count] = int(octets_list[count])
if not 0 <= octets_list[count] <= 255 :
raise Exception
except :
raise ValidatorError("Argument \"%s\" is not valid IPv4 netmask" % (arg))
else :
raise ValidatorError("Argument \"%s\" is not valid IPv4 netmask" % (arg))
return (arg, octets_list)
def validMacAddress(arg) :
arg = str(arg).strip()
octets_list = arg.split(":")
if len(octets_list) != 6 :
raise ValidatorError("Argument \"%s\" is not valid MAC address" % (arg))
for count in xrange(6) :
try :
octets_list[count] = int(octets_list[count], 16)
if not 0 <= octets_list[count] <= 255 :
raise Exception
except :
raise ValidatorError("Argument \"%s\" is not valid MAC address" % (arg))
return (arg, octets_list)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment