def _get_free_port(host):
 s = socket.socket()
 s.bind((host, 0))
 port = s.getsockname()[1]
 s.close()
 return port

def _get_free_port ( host ) : s = socket . socket ( ) s . bind ( ( host , 0 ) ) port = s . getsockname ( ) [ 1 ] s . close ( ) return port
gets a free port by opening a socket .