9 lines
220 B
Python
9 lines
220 B
Python
"""测试辅助:获取空闲 TCP 端口。"""
|
|
import socket
|
|
|
|
|
|
def free_port() -> int:
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
s.bind(("127.0.0.1", 0))
|
|
return s.getsockname()[1]
|