()
return port
Find a free port resulting from using port 0.
Find a free port resulting from using port 0.
Find a free port resulting from using port 0 .
def get_free_port():
    """Find a free port resulting from using port 0."""
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 0))
    port = sock.getsockname()[1]
    sock.close()
    return port
def get_free_port ( ) : sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) sock . bind ( ( 'localhost' , 0 ) ) port = sock . getsockname ( ) [ 1 ] sock . close ( ) return port