aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-07-09 17:32:42 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-07-09 17:32:42 +0200
commitb64ac40ac0cec48e525c83d16e08f47b1ef8b81f (patch)
tree22a29c31d0d708a2a2f58550c30021e4c8e42205 /test
parenttest: print test timings (diff)
downloadcimple-b64ac40ac0cec48e525c83d16e08f47b1ef8b81f.tar.gz
cimple-b64ac40ac0cec48e525c83d16e08f47b1ef8b81f.zip
test: attempt to fix random port selection again
Diffstat (limited to '')
-rw-r--r--test/py/lib/net.py14
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