mopidy / mopidy
1 2
import logging
2 2
import re
3 2
import socket
4

5 2
logger = logging.getLogger(__name__)
6

7

8 2
def try_ipv6_socket():
9
    """Determine if system really supports IPv6"""
10 2
    if not socket.has_ipv6:
11 2
        return False
12 2
    try:
13 2
        socket.socket(socket.AF_INET6).close()
14 2
        return True
15 2
    except OSError as exc:
16 2
        logger.debug(
17
            f"Platform supports IPv6, but socket creation failed, "
18
            f"disabling: {exc}"
19
        )
20 2
    return False
21

22

23
#: Boolean value that indicates if creating an IPv6 socket will succeed.
24 2
has_ipv6 = try_ipv6_socket()
25

26

27 2
def format_hostname(hostname):
28
    """Format hostname for display."""
29 2
    if has_ipv6 and re.match(r"\d+.\d+.\d+.\d+", hostname) is not None:
30 2
        hostname = f"::ffff:{hostname}"
31 2
    return hostname

Read our documentation on viewing source code .

Loading