mirror of
https://github.com/GNS3/gns3-server.git
synced 2024-12-19 12:57:56 +00:00
Remove find_unused_port from the attic.
This commit is contained in:
parent
7a19c9062e
commit
ba91cbaac0
@ -30,50 +30,6 @@ import logging
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP", ignore_ports=[]):
|
||||
"""
|
||||
Finds an unused port in a range.
|
||||
|
||||
:param start_port: first port in the range
|
||||
:param end_port: last port in the range
|
||||
:param host: host/address for bind()
|
||||
:param socket_type: TCP (default) or UDP
|
||||
:param ignore_ports: list of port to ignore within the range
|
||||
"""
|
||||
|
||||
if end_port < start_port:
|
||||
raise Exception("Invalid port range {}-{}".format(start_port, end_port))
|
||||
|
||||
if socket_type == "UDP":
|
||||
socket_type = socket.SOCK_DGRAM
|
||||
else:
|
||||
socket_type = socket.SOCK_STREAM
|
||||
|
||||
last_exception = None
|
||||
for port in range(start_port, end_port + 1):
|
||||
if port in ignore_ports:
|
||||
continue
|
||||
try:
|
||||
if ":" in host:
|
||||
# IPv6 address support
|
||||
with socket.socket(socket.AF_INET6, socket_type) as s:
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind((host, port)) # the port is available if bind is a success
|
||||
else:
|
||||
with socket.socket(socket.AF_INET, socket_type) as s:
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
s.bind((host, port)) # the port is available if bind is a success
|
||||
return port
|
||||
except OSError as e:
|
||||
last_exception = e
|
||||
if port + 1 == end_port:
|
||||
break
|
||||
else:
|
||||
continue
|
||||
|
||||
raise Exception("Could not find a free port between {} and {} on host {}, last exception: {}".format(start_port, end_port, host, last_exception))
|
||||
|
||||
|
||||
def wait_socket_is_ready(host, port, wait=2.0, socket_timeout=10):
|
||||
"""
|
||||
Waits for a socket to be ready for wait time.
|
||||
|
Loading…
Reference in New Issue
Block a user