mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-13 01:20:58 +00:00
Change find an unused port.
This commit is contained in:
parent
8c47522a18
commit
f5540ee147
@ -50,6 +50,7 @@ def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP",
|
|||||||
else:
|
else:
|
||||||
socket_type = socket.SOCK_STREAM
|
socket_type = socket.SOCK_STREAM
|
||||||
|
|
||||||
|
last_exception = None
|
||||||
for port in range(start_port, end_port + 1):
|
for port in range(start_port, end_port + 1):
|
||||||
if port in ignore_ports:
|
if port in ignore_ports:
|
||||||
continue
|
continue
|
||||||
@ -63,15 +64,16 @@ def find_unused_port(start_port, end_port, host='127.0.0.1', socket_type="TCP",
|
|||||||
s.bind((host, port)) # the port is available if bind is a success
|
s.bind((host, port)) # the port is available if bind is a success
|
||||||
return port
|
return port
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == errno.EADDRINUSE: # socket already in use
|
last_exception = e
|
||||||
|
if e.errno == errno.EADDRINUSE or e.errno == errno.EACCES: # socket already in use or permission denied
|
||||||
if port + 1 == end_port:
|
if port + 1 == end_port:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise Exception("Could not find an unused port: {}".format(e))
|
raise Exception("Could not find an unused port between {} and {}: {}".format(start_port, end_port, e))
|
||||||
|
|
||||||
raise Exception("Could not find a free port between {0} and {1}".format(start_port, end_port))
|
raise Exception("Could not find a free port between {0} and {1}, last exception: {}".format(start_port, end_port, last_exception))
|
||||||
|
|
||||||
|
|
||||||
def wait_socket_is_ready(host, port, wait=2.0, socket_timeout=10):
|
def wait_socket_is_ready(host, port, wait=2.0, socket_timeout=10):
|
||||||
|
Loading…
Reference in New Issue
Block a user