12 lines
241 B
Python
Raw Permalink Normal View History

2025-07-30 11:57:45 +08:00
import socket
from contextlib import closing
def get_open_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
s.listen(1)
port = s.getsockname()[1]
return port