Skip to content
This repository was archived by the owner on Jan 23, 2026. It is now read-only.

Commit 152f574

Browse files
NickCaogithub-actions[bot]
authored andcommitted
Consolidate impl of get_ip_address
(cherry picked from commit e812ef0)
1 parent a0334dc commit 152f574

3 files changed

Lines changed: 17 additions & 23 deletions

File tree

  • packages
    • jumpstarter-driver-http/jumpstarter_driver_http
    • jumpstarter-driver-tftp/jumpstarter_driver_tftp
    • jumpstarter/jumpstarter/common

packages/jumpstarter-driver-http/jumpstarter_driver_http/driver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from aiohttp import web
77
from jumpstarter_driver_opendal.driver import Opendal
88

9-
from jumpstarter.common.ipaddress import get_default_ip
9+
from jumpstarter.common.ipaddress import get_ip_address
1010
from jumpstarter.driver import Driver, export
1111

1212

@@ -42,7 +42,7 @@ def __post_init__(self):
4242
]
4343
)
4444
if self.host is None:
45-
self.host = get_default_ip(logger=self.logger)
45+
self.host = get_ip_address(logger=self.logger)
4646

4747
@classmethod
4848
def client(cls) -> str:

packages/jumpstarter-driver-tftp/jumpstarter_driver_tftp/driver.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import os
3-
import socket
43
import threading
54
from dataclasses import dataclass, field
65
from typing import Optional
@@ -9,7 +8,7 @@
98

109
from jumpstarter_driver_tftp.server import TftpServer
1110

12-
from jumpstarter.common.ipaddress import get_default_ip
11+
from jumpstarter.common.ipaddress import get_ip_address
1312
from jumpstarter.driver import Driver, export
1413

1514

@@ -56,7 +55,7 @@ def __post_init__(self):
5655
self.storage = self.children["storage"]
5756

5857
if self.host == "":
59-
self.host = get_default_ip(logger=self.logger)
58+
self.host = get_ip_address(logger=self.logger)
6059

6160
@classmethod
6261
def client(cls) -> str:
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import logging
22
import socket
3+
from ipaddress import ip_address
34

45

5-
def get_ip_address() -> str:
6+
def get_ip_address(logger: logging.Logger | None = None) -> str:
67
"""Get the IP address of the host machine"""
78
# Try to get the IP address using the hostname
89
hostname = socket.gethostname()
910
address = socket.gethostbyname(hostname)
10-
# If it returns a bogus address, do it the hard way
11-
if not address or address.startswith("127."):
12-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
13-
s.connect(("1.1.1.1", 0))
14-
address = s.getsockname()[0]
15-
return address
16-
11+
# If it returns nothing or a loopbadck address, do it the hard way
12+
if not address or ip_address(address).is_loopback:
13+
try:
14+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
15+
s.connect(("192.175.48.1", 53)) # AS112
16+
return s.getsockname()[0]
17+
except Exception:
18+
if logger:
19+
logger.warning("Could not determine default IP address, falling back to 0.0.0.0")
20+
return "0.0.0.0"
1721

18-
def get_default_ip(logger: logging.Logger | None = None):
19-
"""Get the IP address of the default route interface"""
20-
try:
21-
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
22-
s.connect(("8.8.8.8", 80))
23-
return s.getsockname()[0]
24-
except Exception:
25-
if logger:
26-
logger.warning("Could not determine default IP address, falling back to 0.0.0.0")
27-
return "0.0.0.0"
22+
return address

0 commit comments

Comments
 (0)