import socket


def IPv4(ip_addr):
    """
    Validate a string as an IPv4 address. See `man 3 inet` for details.
    """
    ip_addr = str(ip_addr)

    try:
        socket.inet_aton(ip_addr)
    except socket.error:
        raise ValueError('Invalid IPv4 address: %s' % ip_addr)

    return ip_addr