feat: Enhance echo example to dynamically find free ports and improve address handling

- Added a function to find a free port on localhost.
- Updated the run function to use the new port finding logic when a non-positive port is provided.
- Modified address printing to handle multiple listen addresses correctly.
- Improved the get_available_interfaces function to ensure the IPv4 loopback address is included.
This commit is contained in:
yashksaini-coder
2025-08-22 11:48:37 +05:30
parent 5b9bec8e28
commit ed2716c1bf
2 changed files with 25 additions and 10 deletions

View File

@ -67,6 +67,10 @@ def get_available_interfaces(port: int, protocol: str = "tcp") -> list[Multiaddr
seen_v4.add(ip)
addrs.append(Multiaddr(f"/ip4/{ip}/{protocol}/{port}"))
# Ensure IPv4 loopback is always included when IPv4 interfaces are discovered
if seen_v4 and "127.0.0.1" not in seen_v4:
addrs.append(Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}"))
seen_v6: set[str] = set()
for ip in _safe_get_network_addrs(6):
seen_v6.add(ip)