From 4d1948c68f7cd662f52cc008e6b181a40b13b88a Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 30 Dec 2020 12:39:57 +0100 Subject: [PATCH 1/4] renamed find_port to find_free_port --- pype/modules/rest_api/rest_api.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pype/modules/rest_api/rest_api.py b/pype/modules/rest_api/rest_api.py index 2a074fd97a..9afb126b12 100644 --- a/pype/modules/rest_api/rest_api.py +++ b/pype/modules/rest_api/rest_api.py @@ -131,9 +131,9 @@ class RestApiModule(PypeModule, ITrayService): module.rest_api_initialization(self) - def find_port(self): start_port = self.default_port exclude_ports = self.exclude_ports + def find_free_port(port_from, port_to=None, exclude_ports=None, host=None): found_port = None # port check takes time so it's lowered to 100 ports for port in range(start_port, start_port+100): @@ -150,7 +150,9 @@ class RestApiModule(PypeModule, ITrayService): return found_port def tray_init(self): - port = self.find_port() + port = self.find_free_port( + self.default_port, self.default_port + 100, self.exclude_ports + ) self.rest_api_url = "http://localhost:{}".format(port) self.rest_api_thread = RestApiThread(self, port) self.register_statics("/res", resources.RESOURCES_DIR) From 5e08f26ac2c4014b78a0af816b10ec5e67146607 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 30 Dec 2020 12:41:18 +0100 Subject: [PATCH 2/4] free port is checked in faster way --- pype/modules/rest_api/rest_api.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/pype/modules/rest_api/rest_api.py b/pype/modules/rest_api/rest_api.py index 9afb126b12..3155038666 100644 --- a/pype/modules/rest_api/rest_api.py +++ b/pype/modules/rest_api/rest_api.py @@ -131,22 +131,37 @@ class RestApiModule(PypeModule, ITrayService): module.rest_api_initialization(self) - start_port = self.default_port - exclude_ports = self.exclude_ports + @staticmethod def find_free_port(port_from, port_to=None, exclude_ports=None, host=None): + # Check only entered port if `port_to` is not defined + if port_to is None: + port_to = port_from + + # Excluded ports (e.g. reserved for other servers/clients) + if exclude_ports is None: + exclude_ports = [] + + # Default host is localhost but it is possible to look for other hosts + if host is None: + host = "localhost" + found_port = None # port check takes time so it's lowered to 100 ports - for port in range(start_port, start_port+100): + for port in range(port_from, port_to + 1): if port in exclude_ports: continue + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - result = sock.connect_ex(("localhost", port)) - if result != 0: + try: + sock.bind((host, port)) + sock.close() found_port = port + except OSError: + continue + if found_port is not None: break - if found_port is None: - return None + return found_port def tray_init(self): From d2b1acbc7cfe7653404467a9f62a5afb2379ae59 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 30 Dec 2020 12:41:25 +0100 Subject: [PATCH 3/4] added docstring --- pype/modules/rest_api/rest_api.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pype/modules/rest_api/rest_api.py b/pype/modules/rest_api/rest_api.py index 3155038666..3861740aab 100644 --- a/pype/modules/rest_api/rest_api.py +++ b/pype/modules/rest_api/rest_api.py @@ -133,6 +133,20 @@ class RestApiModule(PypeModule, ITrayService): @staticmethod def find_free_port(port_from, port_to=None, exclude_ports=None, host=None): + """Find available socket port from entered range. + + It is also possible to only check if entered port is available. + + Args: + port_from (int): Port number which is checked as first. + port_to (int): Last port that is checked in sequence from entered + `port_from`. Only `port_from` is checked if is not entered. + Nothing is processed if is equeal to `port_from`! + exclude_ports (list, tuple, set): List of ports that won't be + checked form entered range. + host (str): Host where will check for free ports. Set to + "localhost" by default. + """ # Check only entered port if `port_to` is not defined if port_to is None: port_to = port_from From 579e5a14960683d0433ede435b6ee09b20d02d07 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Wed, 30 Dec 2020 13:14:39 +0100 Subject: [PATCH 4/4] find free port is pyhon 2 compatible --- pype/modules/rest_api/rest_api.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pype/modules/rest_api/rest_api.py b/pype/modules/rest_api/rest_api.py index 3861740aab..a30402b5fe 100644 --- a/pype/modules/rest_api/rest_api.py +++ b/pype/modules/rest_api/rest_api.py @@ -160,18 +160,22 @@ class RestApiModule(PypeModule, ITrayService): host = "localhost" found_port = None - # port check takes time so it's lowered to 100 ports for port in range(port_from, port_to + 1): if port in exclude_ports: continue - with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: - try: - sock.bind((host, port)) + sock = None + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.bind((host, port)) + found_port = port + + except socket.error: + continue + + finally: + if sock: sock.close() - found_port = port - except OSError: - continue if found_port is not None: break