mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-29 10:06:27 -05:00
* Tighten up ruff ignore list * Okay that fix was indeed unsafe * enable type-name-incorrect-variance * enable literal-membership * enable non-augmented-assignment * enable useless-return * enable global-variable-not-assigned * - * fixup * Clean up roff.toml from annotations * use ignore instead of explicit re-export * use more descriptive names --------- Co-authored-by: Kyle Altendorf <sda@fstab.net>
25 lines
645 B
Python
25 lines
645 B
Python
from __future__ import annotations
|
|
|
|
import socket
|
|
from contextlib import closing
|
|
|
|
recent_ports: set[int] = set()
|
|
|
|
|
|
def find_available_listen_port(name: str = "free") -> int:
|
|
while True:
|
|
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
|
|
try:
|
|
s.bind(("", 0))
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
port = s.getsockname()[1]
|
|
except OSError:
|
|
continue
|
|
|
|
if port in recent_ports:
|
|
continue
|
|
|
|
recent_ports.add(port)
|
|
print(f"{name} port: {port}")
|
|
return port # type: ignore
|