Click here to Skip to main content
15,915,093 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to validate the IP address for 1pv4(4 digit ip like 10.10.10.10)
if i gave 10.12.13 only and also no ip address .

What I have tried:

a = element.split('.')
    if len(a) != 4:
        return False
    for x in a:
        if not x.isdigit() or x == '':
            return False
        i = int(x)
        if i < 0 or i > 255:
            return False
    return True
Posted
Updated 9-Aug-17 0:42am
Comments
Patrice T 9-Aug-17 5:28am    
What is your problem with this code?
Kornfeld Eliyahu Peter 9-Aug-17 6:46am    
By looking at your code - if this is the exact way you have it, all your problem is the indentation from the second line and on...
Python - very cleverly - manages scopes based on those indentation, so indenting the 'if' will try to create a scope under 'a=' which is impossible and cause an error...

1 solution

Try:
Python
def iptest(element):
    a = element.split('.')
    if len(a) != 4:
        return False
    for x in a:
        if not x.isdigit() or x == '':
            return False
        i = int(x)
        if i < 0 or i > 255:
            return False
    return True
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900