From b64ac40ac0cec48e525c83d16e08f47b1ef8b81f Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 9 Jul 2023 17:32:42 +0200 Subject: test: attempt to fix random port selection again --- test/py/lib/net.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/test/py/lib/net.py b/test/py/lib/net.py index 816dd99..326bd24 100644 --- a/test/py/lib/net.py +++ b/test/py/lib/net.py @@ -4,18 +4,12 @@ # Distributed under the MIT License. from contextlib import closing -import random import socket -def port_is_open(host, port): - with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: - return sock.connect_ex((host, port)) == 0 - - def random_unused_port(): - while True: - port = random.randint(20000, 40000) - if port_is_open('127.0.0.1', port): - continue + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(('0.0.0.0', 0)) + port = sock.getsockname()[1] return port -- cgit v1.2.3