I am trying to validate if the string of numbers is valid IP address or not.
There are mere 2 logics that i am using as shown in code-
- The length of
ipnosshould be 4 after thesplit('.') - The strings should be numbers only.
But looks like it is NOT giving me correct result.
For ip='123.456.78.09' : It should result True but it returning False
def validate(ip):
ipnos=ip.split('.')
validnos=[0,1,2,3,4,5,6,7,8,9]
false_counter=0
if len(ipnos) != 4:
false_counter =1
for sublist in ipnos:
for nos in range(len(sublist)):
if sublist[nos] not in validnos:
false_counter =1
if false_counter>0:
return False
else:
return True
ip='123.456.78.09'
validate(ip)
CodePudding user response:
The type of sublist[nos] is <class 'str'>, not <class 'int'>.
So, you should change the validnos to next to fix it:
validnos=['0','1','2','3','4','5','6','7','8','9']
