Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
beat
beat.core
Commits
5e596c3f
Commit
5e596c3f
authored
Sep 28, 2018
by
Samuel GAIST
Browse files
[utils] Added helper method to get free port from range
parent
88acde4f
Changes
1
Hide whitespace changes
Inline
Side-by-side
beat/core/utils.py
View file @
5e596c3f
...
@@ -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
]
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment