aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/src/lib/net.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/src/lib/net.py')
-rw-r--r--test/src/lib/net.py15
1 files changed, 15 insertions, 0 deletions
diff --git a/test/src/lib/net.py b/test/src/lib/net.py
new file mode 100644
index 0000000..06bfda0
--- /dev/null
+++ b/test/src/lib/net.py
@@ -0,0 +1,15 @@
+# Copyright (c) 2023 Egor Tensin <egor@tensin.name>
+# 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 socket
+
+
+def random_unused_port():
+ 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