From 7f5f6019b2f9a46d676f29885cd2dd28f9d63533 Mon Sep 17 00:00:00 2001 From: "Simental Magana, Marcos" Date: Tue, 2 Feb 2016 15:58:53 -0600 Subject: [PATCH] Add function to check if a given port is open Signed-off-by: Simental Magana, Marcos --- clearstack/common/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clearstack/common/util.py b/clearstack/common/util.py index 0ecddc2..dc51ccd 100644 --- a/clearstack/common/util.py +++ b/clearstack/common/util.py @@ -52,6 +52,13 @@ def _print_error_message(self, e, file_name): file_name)) +def port_open(port): + """Return True if given port is already open in localhost. + Return False otherwise.""" + sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + return (sck.connect_ex(('127.0.0.1', port)) == 0) + + def setup_debugging(debug, is_remote_host=True): if not os.path.isdir(LOG_DIR): os.makedirs(LOG_DIR)