Files
chia-blockchain/chia/simulator/socket.py
6c90a76b56 Tighten up ruff ignore list (#18837)
* 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>
2024-11-12 12:19:31 -07:00

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