import socket def get_ip_address(): try: # Connect to an external address — no data is actually sent s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) # Google's DNS ip = s.getsockname()[0] s.close() return ip except Exception: return "127.0.0.1" # fallback to localhost print("Local IP address:", get_ip_address())