chia-blockchain/chia/simulator/socket.py
Kyle Altendorf 3b084a165b
configure isort to add the future annotations import (#13327)
* configure isort to add the future annotations import

* apply the new isort setting

* remove type ignores for new mypy (#13539)

https://pypi.org/project/mypy/0.981/

* another
2022-09-30 03:40:22 -05:00

28 lines
610 B
Python

from __future__ import annotations
import secrets
import socket
from typing import Set
recent_ports: Set[int] = set()
def find_available_listen_port(name: str = "free") -> int:
global recent_ports
while True:
port = secrets.randbelow(0xFFFF - 1024) + 1024
if port in recent_ports:
continue
with socket.socket() as s:
try:
s.bind(("127.0.0.1", port))
except OSError:
recent_ports.add(port)
continue
recent_ports.add(port)
print(f"{name} port: {port}")
return port