in src/dubbo/utils.py [0:0]
def get_local_address() -> Optional[str]:
"""
Find first valid IP from local network card.
:return: The local IP address. If not found, return None.
:rtype: str
"""
try:
# use psutil to get the local IP address
for iface_name, iface_addrs in psutil.net_if_addrs().items():
for addr in iface_addrs:
# only consider IPv4 address
if addr.family == socket.AF_INET:
# ignore the loopback address and check if the IP address is reachable
if not NetworkUtils.is_loopback_address(addr.address) and NetworkUtils.is_address_reachable(
addr.address
):
return addr.address
except Exception:
pass
# if the local IP address is not found, try to get the IP address using the socket
try:
local_host_ip = socket.gethostbyname(socket.gethostname())
return local_host_ip
except Exception:
pass
return None