Pacific-Design.com

    
Home Index

1. Python

2. Validate IP Address

Python / Validate IP Address /

Validate IP Address

#!/usr/bin/python
#------------------------------------------------#
def validate_ip_address(ip):

    parts = ip.split('.')

    if len(parts) != 4:
        return False
    
    for part in parts:

        if not part.isdigit():
            return False

        i = int(part)
        if i < 0 or i > 255:
            return False

    return True


#------------------------------------------------#
assert validate_ip_address('192.168.0.2') == True
assert validate_ip_address('192.IP.0.2') == False 
print('passed')