Comment 1 for bug 1538812

Florian Haas (fghaas) wrote :

So, this works just fine:

@cached
def get_host_ip(hostname=None):
    if config('prefer-ipv6'):
        return get_ipv6_addr()[0]

    hostname = hostname or unit_get('private-address')
    try:
        # Test to see if already an IPv4 address
        socket.inet_aton(hostname)
        return hostname
    except socket.error:
        return socket.gethostbyname(hostname)