From ecd758227d45a0b64f4dc400a9e6a48c4e3fa220 Mon Sep 17 00:00:00 2001 From: Denis Zatyagov Date: Mon, 27 Jul 2026 16:32:36 +0200 Subject: [PATCH] make get_local_ip work offline and on hotspots --- teleop/__init__.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/teleop/__init__.py b/teleop/__init__.py index eb36af4..616fe4b 100644 --- a/teleop/__init__.py +++ b/teleop/__init__.py @@ -10,21 +10,31 @@ import transforms3d as t3d import numpy as np import json +import subprocess TF_RUB2FLU = np.array([[0, 0, -1, 0], [-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1]]) THIS_DIR = os.path.dirname(os.path.realpath(__file__)) -def get_local_ip(): +def get_local_ip() -> str: + # Try connecting to an external address first (requires default route / gateway) try: - # Connect to an external address (doesn't actually send data) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - s.connect(("8.8.8.8", 80)) # Google DNS as a dummy target + # Doesn't actually send data, but forces OS to choose an interface with a route + s.connect(("8.8.8.8", 80)) local_ip = s.getsockname()[0] s.close() return local_ip - except Exception as e: - return f"Error: {e}" + except Exception: + pass + + # Offline/hotspot fallback using 'hostname -I' (Linux-only) + try: + ips = subprocess.check_output(["hostname", "-I"]).decode().strip().split() + return ips[0] if ips else "127.0.0.1" + except Exception: + return "127.0.0.1" + def are_close(a, b=None, lin_tol=1e-9, ang_tol=1e-9):