From 91775880abd5b36966bae9d6b5b4eaf519fe3ab5 Mon Sep 17 00:00:00 2001
From: Fabian Wachsmann <k204210@l40147.lvt.dkrz.de>
Date: Wed, 12 Feb 2025 11:52:12 +0100
Subject: [PATCH] With port finder

---
 scripts/cloudify_tco2reg.py | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/scripts/cloudify_tco2reg.py b/scripts/cloudify_tco2reg.py
index 93ece8e..3e3c12a 100644
--- a/scripts/cloudify_tco2reg.py
+++ b/scripts/cloudify_tco2reg.py
@@ -10,6 +10,7 @@ import os
 import subprocess
 #import hdf5plugin
 import argparse
+import socket
 
 def parse_args():
     parser = argparse.ArgumentParser(
@@ -51,7 +52,19 @@ if not os.path.isfile(ssl_keyfile) or not os.path.isfile(ssl_certfile):
 
     subprocess.run(openssl_cmd, check=True)
 
-port=9010
+def is_port_free(port, host="localhost"):
+    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
+        return s.connect_ex((host, port)) != 0  # Returns True if the port is free
+
+def find_free_port(start=5000, end=5100, host="localhost"):
+    for port in range(start, end + 1):
+        if is_port_free(port, host):
+            return port
+    return None  # No free ports found
+
+port = find_free_port(9000,9100)
+if not port:
+    raise ValueError("Could not find a free port for service")
 
 nest_asyncio.apply()
 chunks={}
-- 
GitLab