15 lines
320 B
Python
15 lines
320 B
Python
|
import socket
|
||
|
|
||
|
def get_open_port():
|
||
|
""" Retrieves an open port on the system. """
|
||
|
|
||
|
# Bind a socket to a random port. We can then ask for the port number,
|
||
|
# and return it.
|
||
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
s.bind(("", 0))
|
||
|
s.listen(1)
|
||
|
port = s.getsockname()[1]
|
||
|
s.close()
|
||
|
|
||
|
return port
|