Skip to content

How to Get Hostname and IP Address in Python

Learn reliable Python methods to retrieve hostname, local (LAN) IP, public (WAN) IP, and all network interfaces using socket, urllib, and netifaces with code examples.

A machine typically has multiple IP addresses: loopback (127.0.0.1), local network (LAN), and public internet (WAN). Python's socket module provides the tools to retrieve each.

Getting the Hostname

The hostname is the human-readable name identifying your computer on the network:

import socket

hostname = socket.gethostname()
print(f"Hostname: {hostname}")

Getting the Local IP Address (LAN)

Your local network address (typically 192.168.x.x or 10.x.x.x) requires the "UDP trick" for reliable detection:

import socket

def get_local_ip():
    """Get local IP address using UDP socket trick."""
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('8.8.8.8', 80))
        return s.getsockname()[0]
    except Exception:
        return '127.0.0.1'
    finally:
        s.close()

print(f"Local IP: {get_local_ip()}")

This method works by asking the OS which interface would route traffic to an external address, without actually sending any data.

Getting the Public IP Address (WAN)

Your public IP is assigned by your ISP and requires querying an external service:

import urllib.request

def get_public_ip():
    """Get public IP address from external service."""
    try:
        response = urllib.request.urlopen('https://api.ipify.org', timeout=5)
        return response.read().decode('utf-8')
    except Exception as e:
        return f"Error: {e}"

print(f"Public IP: {get_public_ip()}")

Using the requests library for more control:

import requests

def get_public_ip():
    """Get public IP with detailed info."""
    try:
        response = requests.get('https://ipapi.co/json/', timeout=5)
        data = response.json()
        return {
            'ip': data.get('ip'),
            'city': data.get('city'),
            'country': data.get('country_name'),
            'isp': data.get('org')
        }
    except requests.RequestException as e:
        return None

info = get_public_ip()
print(info)

Getting All Network Interfaces

List all IP addresses on all network interfaces:

import socket

def get_all_ips():
    """Get all IP addresses for this host."""
    hostname = socket.gethostname()
    try:
        addresses = socket.getaddrinfo(hostname, None, socket.AF_INET)
        return list(set(addr[4][0] for addr in addresses))
    except socket.gaierror:
        return ['127.0.0.1']

print(get_all_ips())

For detailed interface information, use the netifaces library:

import netifaces

def get_interface_details():
    """Get detailed info for all network interfaces."""
    interfaces = {}
    
    for iface in netifaces.interfaces():
        addrs = netifaces.ifaddresses(iface)
        
        if netifaces.AF_INET in addrs:
            interfaces[iface] = {
                'ipv4': addrs[netifaces.AF_INET][0].get('addr'),
                'netmask': addrs[netifaces.AF_INET][0].get('netmask')
            }
    
    return interfaces

print(get_interface_details())

Complete Network Information Function

Combine all methods into a comprehensive function:

import socket
import urllib.request

def get_network_info():
    """Get comprehensive network information."""
    info = {}
    
    info['hostname'] = socket.gethostname()
    
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        info['local_ip'] = s.getsockname()[0]
        s.close()
    except Exception:
        info['local_ip'] = '127.0.0.1'
    
    try:
        response = urllib.request.urlopen('https://api.ipify.org', timeout=5)
        info['public_ip'] = response.read().decode('utf-8')
    except Exception:
        info['public_ip'] = 'Unavailable'
    
    return info

print(get_network_info())

Checking If IP Is Local or Public

import ipaddress

def is_private_ip(ip_string):
    """Check if IP address is private (local network)."""
    try:
        ip = ipaddress.ip_address(ip_string)
        return ip.is_private
    except ValueError:
        return False

print(is_private_ip('192.168.1.1'))   # True
print(is_private_ip('10.0.0.1'))      # True
print(is_private_ip('8.8.8.8'))       # False
print(is_private_ip('172.16.0.1'))    # True

Quick Reference

Goal Method Example Result
Hostname socket.gethostname() "my-laptop"
Local IP UDP trick to 8.8.8.8 "192.168.1.105"
Public IP External API call "203.0.113.42"
All IPs socket.getaddrinfo() List of addresses

Summary

  • Use socket.gethostname() for the machine name.
  • For local IP, the UDP socket trick connecting to 8.8.8.8 is the most reliable cross-platform method.
  • Public IP requires querying an external service like api.ipify.org.
  • Avoid socket.gethostbyname(hostname) as it often returns loopback on Linux systems.
Navigation