Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
local_ip.py41 linesDownload Raw Back to net
1from __future__ import annotations2 3import socket4 5 6def get_local_ip(reachable: str = "8.8.8.8") -> str | None:7    """8    Get the default local outgoing IPv4 address without sending any packets.9    This will fail if the target address is known to be unreachable.10    We use Google DNS's IPv4 address as the default.11    """12    # https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib13    s = None14    try:15        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)16        s.connect((reachable, 80))17        return s.getsockname()[0]  # pragma: no cover18    except OSError:19        return None  # pragma: no cover20    finally:21        if s is not None:22            s.close()23 24 25def get_local_ip6(reachable: str = "2001:4860:4860::8888") -> str | None:26    """27    Get the default local outgoing IPv6 address without sending any packets.28    This will fail if the target address is known to be unreachable.29    We use Google DNS's IPv6 address as the default.30    """31    s = None32    try:33        s = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)34        s.connect((reachable, 80))35        return s.getsockname()[0]  # pragma: no cover36    except OSError:  # pragma: no cover37        return None38    finally:39        if s is not None:40            s.close()41 
codekingpro/portable-devtools · Team Ai