"""Shared free-port helper for svc tests (not a test module).""" import socket def free_test_port(): """Return a port the kernel reports free on 127.0.0.1 right now. Each call opens a fresh reservation and closes it before returning, so the caller (e.g. `svc deploy`) can bind the port itself. Ports are not pooled. """ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as reservation: reservation.bind(("127.0.0.1", 0)) return reservation.getsockname()[1]