12 lines
241 B
Python
12 lines
241 B
Python
|
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
|