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():
socket.SOCK_STREAM)) as s:
s.bind(('', 0))
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:
sock.bind(('', port))
except socket.error as e:
return sock.getsockname()[1]
Supports Markdown
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