mower-ng/arknights_mower/utils/network.py
Zhao Zuohong e6ba5600ed
Some checks failed
代码格式检查与测试 / ruff (push) Has been cancelled
代码格式检查与测试 / prettier (push) Has been cancelled
代码格式检查与测试 / unittest (push) Has been cancelled
迁移仓库
2024-08-09 03:59:24 +08:00

13 lines
361 B
Python

import socket
def is_port_in_use(port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.1)
return s.connect_ex(("localhost", port)) == 0
def get_new_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("localhost", 0))
return s.getsockname()[1]