Module: check_mk
Branch: master
Commit: 6650e0de0e7e11bdacf77c01f51583c01970072d
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6650e0de0e7e11…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Wed Nov 23 17:10:52 2016 +0100
Trying to make free port detection more robust
---
tests/testlib/__init__.py | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index be63e83..d479d74 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -381,12 +381,20 @@ class Site(object):
def get_free_port_from(self, port):
- print "Trying port %d" % port
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- while sock.connect_ex(('127.0.0.1', port)) == 0:
- print "Port %d is already used, trying next" % port
- port += 1
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ while True:
+ try:
+ print "Trying port %d" % port
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ sock.bind(("127.0.0.1", port))
+ sock.listen(1)
+ sock.close()
+
+ break # found a bindable port
+ except socket.error, e:
+ print "Failed to bind 127.0.0.1:%d: %s" % (port, e)
+ port += 1
+
print "Using port %d" % port
return port