mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2024-09-20 16:08:51 +03:00
3b084a165b
* 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
28 lines
610 B
Python
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
|