Commit 5e596c3f authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[utils] Added helper method to get free port from range

parent 88acde4f
...@@ -103,3 +103,17 @@ def find_free_port(): ...@@ -103,3 +103,17 @@ def find_free_port():
socket.SOCK_STREAM)) as s: socket.SOCK_STREAM)) as s:
s.bind(('', 0)) s.bind(('', 0))
return s.getsockname()[1] return s.getsockname()[1]
def find_free_port_in_range(min_port, max_port):
'''Returns the value of a free port in range'''
for port in range(min_port, max_port):
with contextlib.closing(socket.socket(socket.AF_INET,
socket.SOCK_STREAM)) as sock:
try:
sock.bind(('', port))
except socket.error as e:
continue
else:
return sock.getsockname()[1]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment