Check if IP is valid

From DevOps Notebook

Method 1:

import socket
try:
    socket.inet_aton(addr)
    # legal
except socket.error:
    # Not legal

Method 2:

def validate_ip(s):
    a = s.split('.')
    if len(a) != 4:
        return False
    for x in a:
        if not x.isdigit():
            return False
        i = int(x)
        if i < 0 or i > 255:
            return False
    return True