Skip to content

Commit ed91ee0

Browse files
refactor(app): 804 refactored find_free_port() in address_validation.py
1 parent 75ffb79 commit ed91ee0

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

examples/echo/echo.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import argparse
22
import random
33
import secrets
4-
import socket
54

65
import multiaddr
76
import trio
@@ -25,20 +24,14 @@
2524
info_from_p2p_addr,
2625
)
2726
from libp2p.utils.address_validation import (
27+
find_free_port,
2828
get_available_interfaces,
2929
)
3030

3131
PROTOCOL_ID = TProtocol("/echo/1.0.0")
3232
MAX_READ_LEN = 2**32 - 1
3333

3434

35-
def find_free_port():
36-
"""Find a free port on localhost."""
37-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
38-
s.bind(("", 0)) # Bind to a free port provided by the OS
39-
return s.getsockname()[1]
40-
41-
4235
async def _echo_stream_handler(stream: INetStream) -> None:
4336
try:
4437
peer_id = stream.muxed_conn.peer_id

examples/pubsub/pubsub.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import argparse
22
import logging
3-
import socket
43

54
import base58
65
import multiaddr
@@ -31,6 +30,9 @@
3130
from libp2p.tools.async_service.trio_service import (
3231
background_trio_service,
3332
)
33+
from libp2p.utils.address_validation import (
34+
find_free_port,
35+
)
3436

3537
# Configure logging
3638
logging.basicConfig(
@@ -77,13 +79,6 @@ async def publish_loop(pubsub, topic, termination_event):
7779
await trio.sleep(1) # Avoid tight loop on error
7880

7981

80-
def find_free_port():
81-
"""Find a free port on localhost."""
82-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
83-
s.bind(("", 0)) # Bind to a free port provided by the OS
84-
return s.getsockname()[1]
85-
86-
8782
async def monitor_peer_topics(pubsub, nursery, termination_event):
8883
"""
8984
Monitor for new topics that peers are subscribed to and

libp2p/utils/address_validation.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
import socket
4+
35
from multiaddr import Multiaddr
46

57
try:
@@ -35,6 +37,13 @@ def _safe_get_network_addrs(ip_version: int) -> list[str]:
3537
return []
3638

3739

40+
def find_free_port() -> int:
41+
"""Find a free port on localhost."""
42+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
43+
s.bind(("", 0)) # Bind to a free port provided by the OS
44+
return s.getsockname()[1]
45+
46+
3847
def _safe_expand(addr: Multiaddr, port: int | None = None) -> list[Multiaddr]:
3948
"""
4049
Internal safe expansion wrapper. Returns a list of Multiaddr objects.
@@ -147,4 +156,5 @@ def is_non_loopback(ma: Multiaddr) -> bool:
147156
"get_available_interfaces",
148157
"get_optimal_binding_address",
149158
"expand_wildcard_address",
159+
"find_free_port",
150160
]

0 commit comments

Comments
 (0)