-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportutil.py
More file actions
32 lines (26 loc) · 1.18 KB
/
portutil.py
File metadata and controls
32 lines (26 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"""在首选端口被占用时,从一段连续端口中选取第一个可用的 TCP 监听端口。"""
from __future__ import annotations
import socket
def pick_free_tcp_port(host: str, preferred: int, span: int = 40) -> int:
"""
若 preferred 被占用,在 [preferred, preferred+span) 内找第一个可 bind 的端口。
探测地址与常见监听方式对齐:0.0.0.0 用 0.0.0.0 探测,避免与仅绑定 0.0.0.0 的进程不一致。
"""
if preferred < 1 or preferred > 65535:
raise ValueError("preferred 必须在 1–65535 之间")
probe_host = host
if host in ("", "::"):
probe_host = "127.0.0.1"
elif host == "0.0.0.0":
probe_host = "0.0.0.0"
for p in range(preferred, min(preferred + span, 65536)):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((probe_host, p))
except OSError:
continue
return p
raise RuntimeError(
f"在 {preferred}–{min(preferred + span - 1, 65535)} 范围内没有可用端口,请关闭占用进程或指定其他端口"
)