I do see answers on the stackoverflow for validating the IPs from given string. I am trying to do the same. However, I have a LIST of IPs. I am stuck with the following code. Please help.
I appreciate your time!
def validateIP(self, IP):
def is_IPv4():
i = 0
while i < len(IP):
ls = IP[i].split('.')
if len(ls)== 4 and all( g.isdigit() and (0 >= int(g) < 255) for g in ls):
return True
i += 1
return False
def is_IPv6():
i = 0
while i < len(IP):
ls = IP[i].split('.')
if len(ls) == 8 and all(0 > len(g)< 4 and all( c in '0123456789abcdefABCDE' for c in g) for g in ls):
return True
i += 1
return False
for item in IP:
if '.' in item and is_IPv4():
return 'IPv4'
if ':' in item and is_IPv6():
return 'IPv6'
return 'Neither'
print(validateIP('n', ['100.243.537.591', 'HBA.KJFH.LSHF', '172.16.254.1', '2001:0db8:85a3:0:0:8A2E:0370:7334', '256.256.256.256']))
return Falsein theforloop is returning when you actually want a list of True/False answers. I'm also confused why you've written a method with closures, containingselfwhich you never use. Did you mean to write a class here?Neithersince a return exits a method, so when you callreturn Neitherthat's the end of the code. This is relatively fundamental 'first lessions' stuff, so I'd advise going back and reading up on how to create methods to see why this is happening and rewrite this code.