Here is the code I'm using to validate ip addresses entered in a text
field in a form:
Hope its usefull for someone, perhaps an idea for a requires IS_IP4 or
IS_IP6? :)
def ipcheckv6(ipnummer):
Is_IP = True
splitcounter = 0
if ipnummer.count('::') > 1:
#bad formatted address
Is_IP = False
else:
ipnummer=ipnummer.upper() #to check A to F
parts = ipnummer.split(':')
for part in parts:
if len(part) > 4: #no more then 4 chars
Is_IP = False
elif len(part) == 0: #catch multiple : after eachother
more then :: (to catch like :::)
splitcounter = splitcounter + 1
if splitcounter > 1:
Is_IP = False
else:
#catch alphanumeric chars > F, that's not right in an
ipv6 address
#check if values are within A to F and 0 to 9
#if values are 48 to 57 or 65 to 70 then its a right
one
##anything bigger then F isn't right anyway
for i in range(ord('G'),256):
if part.count(chr(i)) > 0:
Is_IP = False
#anything smaller then zero aint right either
for i in range(0,ord('0')):
if part.count(chr(i)) > 0:
Is_IP = False
# this is : to @ sign between numbers and start of
alphabet
for i in range (58,65):
if part.count(chr(i)) > 0:
Is_IP = False
#catch if less then 8 parts, there has to be at least one
double ::
if splitcounter == 0 and len(parts) < 8 and Is_IP:
Is_IP = False
return (Is_IP)
def ipcheck():
ipnummer = request.vars.values()[0]
data = 'holding value'
Is_IP = True
if ipnummer.count('.') <> 3:
Is_IP = False
else:
parts = ipnummer.split('.')
for part in parts:
try: #to make sure it's an int.
if int(part) < 0 or int(part) > 255:
Is_IP = False
except: #if its not an int, its definately not an IPv4
adress
Is_IP = False
if Is_IP:
data='is ipv4'
else:
#if its not ipv4 lets check for ipv6
if ipnummer.count(':') > 1:
checkipv6 = ipcheckv6(ipnummer)
if not checkipv6:
data = 'Not an IP'
else:
data = 'is ipv6'
else:
data= 'Not an IP'
return dict(data=data)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---