Skip to content

has_ipv6()

Return True if this machine can bind the IPv6 loopback address (::1), otherwise False.

Returns:

Type Description
bool

True when this machine can bind ::1, otherwise False.

Raises:

Type Description
OSError

When the check can't be performed.

Examples:

has_ipv6()
# True
Source code in src/notwork/has_ipv6.py
def has_ipv6() -> bool:
    """
    Return `True` if this machine can bind the IPv6 loopback address (`::1`),
    otherwise `False`.

    Returns:
        `True` when this machine can bind `::1`, otherwise `False`.

    Raises:
        OSError: When the check can't be performed.

    Examples:
        ```python
        has_ipv6()
        # True
        ```
    """
    # Python built with `--disable-ipv6` can still export `AF_INET6` from the
    # system headers, but can't bind IPv6 addresses, so check both.
    if not _PYTHON_HAS_IPV6 or AF_INET6 is None:
        return False

    try:
        with socket(AF_INET6, SOCK_STREAM) as sock:
            sock.bind(("::1", 0))

    except OSError as ex:
        if ex.errno in _NO_IPV6:
            return False

        raise

    return True