refactor(app): 804 refactored find_free_port() in address_validation.py

This commit is contained in:
yashksaini-coder
2025-08-24 23:28:02 +05:30
parent 75ffb791ac
commit ed91ee0c31
3 changed files with 14 additions and 16 deletions

View File

@ -1,7 +1,6 @@
import argparse
import random
import secrets
import socket
import multiaddr
import trio
@ -25,6 +24,7 @@ from libp2p.peer.peerinfo import (
info_from_p2p_addr,
)
from libp2p.utils.address_validation import (
find_free_port,
get_available_interfaces,
)
@ -32,13 +32,6 @@ PROTOCOL_ID = TProtocol("/echo/1.0.0")
MAX_READ_LEN = 2**32 - 1
def find_free_port():
"""Find a free port on localhost."""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0)) # Bind to a free port provided by the OS
return s.getsockname()[1]
async def _echo_stream_handler(stream: INetStream) -> None:
try:
peer_id = stream.muxed_conn.peer_id