diff options
Diffstat (limited to 'test/py/lib/net.py')
-rw-r--r-- | test/py/lib/net.py | 14 |
1 files 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 |