Get Local IP Address in Python Without Third-Party Libraries

How can I find local IP addresses (i.e., 192.168.x.x or 10.0.x.x) in Python platform-independently using only the standard library? I would like to know how to python get ip address without relying on any third-party libraries.

I see! If you’re just starting out with finding the local IP address in Python, one easy way is by using the socket library. This method works across different platforms because you’re essentially binding a socket to an arbitrary address. Here’s a simple approach to get the python get ip address:

import socket

def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))  # Connecting to Google's DNS server
    local_ip = s.getsockname()[0]  # Getting the local IP address
    s.close()
    return local_ip

print("Local IP address:", get_local_ip())

This will give you the local IP by connecting to an external server, which automatically chooses the correct network interface. It’s simple and doesn’t rely on any third-party packages. But keep in mind, it depends on being able to reach an external address like 8.8.8.8 (Google’s DNS).

That’s a good method, but if you’re after something more direct, there’s another approach you can try using socket.gethostname() combined with socket.gethostbyname(). It’s a bit simpler and works great for small local networks. Here’s how you can do it to python get ip address:

import socket

def get_local_ip():
    host = socket.gethostname()  # Get the hostname of your machine
    local_ip = socket.gethostbyname(host)  # Resolve the hostname to an IP address
    return local_ip

print("Local IP address:", get_local_ip())

This way, Python will resolve the IP address based on the local hostname. It’s really platform-independent as well, but just be aware that it might return 127.0.0.1 (localhost) if you’re on certain types of networks.

Both of those are solid methods, but let me take it a step further. If you’re working in a more complex network environment and want to make sure you’re getting the correct python get ip address, you could try inspecting the network interfaces directly. This method might involve a third-party package like netifaces, but it’s still a handy option when you can install it:

import netifaces

def get_local_ip():
    interfaces = netifaces.interfaces()  # List all available network interfaces
    for interface in interfaces:
        addresses = netifaces.ifaddresses(interface)
        if netifaces.AF_INET in addresses:
            ip_info = addresses[netifaces.AF_INET][0]
            # Filter out any non-local IPs (like 127.0.0.1)
            if ip_info["addr"].startswith(("192.", "10.")):  
                return ip_info["addr"]
    return None

print("Local IP address:", get_local_ip())

This will inspect all interfaces and find an IP that matches the typical local address ranges (e.g., 192.168.x.x, 10.x.x.x). It’s a more specific approach, but not strictly part of the standard library unless you use pip install netifaces. However, it’s quite efficient for advanced use cases!