From c0b2472211ebd7938419ee59ac99c2d3056fc803 Mon Sep 17 00:00:00 2001 From: Paul O'Leary McCann Date: Mon, 26 Dec 2022 20:14:32 +0900 Subject: [PATCH] Pass in the host when checking port availability --- spacy/util.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/spacy/util.py b/spacy/util.py index 0af530b2b..4c03f58c0 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -1739,17 +1739,18 @@ def all_equal(iterable): return next(g, True) and not next(g, False) -def is_port_in_use(port): +def is_port_in_use(port, host="localhost"): """Check if localhost:port is in use.""" s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: - s.bind(("localhost", port)) + s.bind((host, port)) return False except socket.error: return True finally: s.close() + def find_available_port(start, host, auto_select_port=False): """Given a starting port and a host, handle finding a port. @@ -1757,22 +1758,19 @@ def find_available_port(start, host, auto_select_port=False): If `auto_select_port` is True, the next free higher port will be used. """ - if not is_port_in_use(start): + if not is_port_in_use(start, host): return start port = start if not auto_select_port: raise ValueError(Errors.E1049.format(port=port)) - while is_port_in_use(port) and port < 65535: + while is_port_in_use(port, host) and port < 65535: port += 1 - if port == 65535 and is_port_in_use(port): + if port == 65535 and is_port_in_use(port, host): raise ValueError(Errors.E1048.format(host=host)) # if we get here, the port changed - warnings.warn( - Warnings.W124.format(host=host, port=start, serve_port=port) - ) + warnings.warn(Warnings.W124.format(host=host, port=start, serve_port=port)) return port -