OSC_ROS2/test/get_ip.py
Alexander Schaefer b67b2a5174 AS: code update
2025-05-07 23:12:12 +02:00

15 lines
416 B
Python

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())