Skip to content

network

dev_tool.tools.network

log = logging.getLogger(__name__) module-attribute

get_local_ip

A function that gets the local IP address of the machine.

Returns:

  • str

    The local IP address, or '127.0.0.1' if it cannot be determined.

Source code in dev_tool/tools/network.py
def get_local_ip() -> str:
    """
    A function that gets the local IP address of the machine.

    :return: The local IP address, or '127.0.0.1' if it cannot be determined.
    """

    try:
        with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as handle:
            connection = ('8.8.8.8', 80)
            handle.connect(connection)
            ip, _ = handle.getsockname()

            return ip
    except Exception:
        return '127.0.0.1'