diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-06 01:03:30 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-06 01:18:03 +0200 |
commit | 0ac982aafad55c246dd65a2eaafc3cb2a1f00027 (patch) | |
tree | fce3c524c75bcf5d4adaf784afca82fdf869ef0b /test/py/lib | |
parent | test: add one more stress test (diff) | |
download | cimple-0ac982aafad55c246dd65a2eaafc3cb2a1f00027.tar.gz cimple-0ac982aafad55c246dd65a2eaafc3cb2a1f00027.zip |
test: try mitigating port clashes
Also, I don't think calling random.seed is necessary.
Diffstat (limited to '')
-rw-r--r-- | test/py/lib/net.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/test/py/lib/net.py b/test/py/lib/net.py new file mode 100644 index 0000000..816dd99 --- /dev/null +++ b/test/py/lib/net.py @@ -0,0 +1,21 @@ +# Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com> +# This file is part of the "cimple" project. +# For details, see https://github.com/egor-tensin/cimple. +# 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 + return port |