# Copyright (c) Microsoft. All rights reserved. import socket from contextlib import closing def get_free_port() -> int: with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: s.bind(("127.0.0.1", 0)) return int(s.getsockname()[1])