summaryrefslogtreecommitdiff
path: root/randport
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-09-26 13:26:20 +0300
committerLars Wirzenius <liw@liw.fi>2017-09-26 13:26:20 +0300
commita32ec0c8e2924aca80b14fccc175f5fc360ec992 (patch)
treefe758a43554276229d47a0e0fe60c65e4fb36731 /randport
parent7f10c4dfa145ff11661106f3aec6c70e95be6af5 (diff)
downloadapifw-a32ec0c8e2924aca80b14fccc175f5fc360ec992.tar.gz
Fix: use random port when running tests
Diffstat (limited to 'randport')
-rwxr-xr-xrandport28
1 files changed, 28 insertions, 0 deletions
diff --git a/randport b/randport
new file mode 100755
index 0000000..d523401
--- /dev/null
+++ b/randport
@@ -0,0 +1,28 @@
+#!/usr/bin/python3
+#
+# Copyright (C) 2017 Lars Wirzenius
+#
+# Pick a random port that is free to be listened on. For testing.
+
+
+import errno
+import random
+import socket
+import sys
+
+
+MAX = 1000
+for i in range(MAX):
+ port = random.randint(1025, 2**15-1)
+ s = socket.socket()
+ try:
+ s.bind(('0.0.0.0', port))
+ except OSError as e:
+ if e.errno == errno.EADDRINUSE:
+ continue
+ raise
+ break
+else:
+ sys.stderr.write("Can't find a free port\n")
+ sys.exit(1)
+sys.stdout.write('{}\n'.format(port))