From 5d4bd0c66826df7ffc634e850c56f3a958002fca Mon Sep 17 00:00:00 2001
From: Fabian Wachsmann <k204210@l40147.lvt.dkrz.de>
Date: Wed, 12 Feb 2025 11:55:11 +0100
Subject: [PATCH] Added a port finder

---
 workshop/1_start-an-app.ipynb                | 14 +++++++++++++-
 workshop/4_kerchunk-input-kerchunk-api.ipynb | 14 +++++++++++++-
 workshop/5_catalog-input.ipynb               | 14 +++++++++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/workshop/1_start-an-app.ipynb b/workshop/1_start-an-app.ipynb
index 7dd0f75..ca7d805 100644
--- a/workshop/1_start-an-app.ipynb
+++ b/workshop/1_start-an-app.ipynb
@@ -162,7 +162,6 @@
    "source": [
     "%%writefile {xpublish_example_script}\n",
     "\n",
-    "port=9010\n",
     "ssl_keyfile=\"/work/bm0021/k204210/cloudify/workshop/key.pem\"\n",
     "ssl_certfile=\"/work/bm0021/k204210/cloudify/workshop/cert.pem\"\n",
     "\n",
@@ -175,7 +174,20 @@
     "import nest_asyncio\n",
     "import sys\n",
     "import os\n",
+    "def is_port_free(port, host=\"localhost\"):\n",
+    "    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n",
+    "        return s.connect_ex((host, port)) != 0  # Returns True if the port is free\n",
     "\n",
+    "def find_free_port(start=5000, end=5100, host=\"localhost\"):\n",
+    "    for port in range(start, end + 1):\n",
+    "        if is_port_free(port, host):\n",
+    "            return port\n",
+    "    return None  # No free ports found\n",
+    "\n",
+    "port = find_free_port(9000,9100)\n",
+    "if not port:\n",
+    "    raise ValueError(\"Could not find a free port for service\")\n",
+    "    \n",
     "nest_asyncio.apply()\n",
     "chunks={}\n",
     "for coord in [\"lon\",\"lat\"]:\n",
diff --git a/workshop/4_kerchunk-input-kerchunk-api.ipynb b/workshop/4_kerchunk-input-kerchunk-api.ipynb
index 04355c0..eccf99d 100644
--- a/workshop/4_kerchunk-input-kerchunk-api.ipynb
+++ b/workshop/4_kerchunk-input-kerchunk-api.ipynb
@@ -53,7 +53,6 @@
    "source": [
     "%%writefile xpublish_references.py\n",
     "\n",
-    "port=9010\n",
     "ssl_keyfile=\"/work/bm0021/k204210/cloudify/workshop/key.pem\"\n",
     "ssl_certfile=\"/work/bm0021/k204210/cloudify/workshop/cert.pem\"\n",
     "\n",
@@ -66,7 +65,20 @@
     "import nest_asyncio\n",
     "import sys\n",
     "import os\n",
+    "def is_port_free(port, host=\"localhost\"):\n",
+    "    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n",
+    "        return s.connect_ex((host, port)) != 0  # Returns True if the port is free\n",
+    "\n",
+    "def find_free_port(start=5000, end=5100, host=\"localhost\"):\n",
+    "    for port in range(start, end + 1):\n",
+    "        if is_port_free(port, host):\n",
+    "            return port\n",
+    "    return None  # No free ports found\n",
     "\n",
+    "port = find_free_port(9000,9100)\n",
+    "if not port:\n",
+    "    raise ValueError(\"Could not find a free port for service\")\n",
+    "    \n",
     "nest_asyncio.apply()\n",
     "\n",
     "if __name__ == \"__main__\":  # This avoids infinite subprocess creation\n",
diff --git a/workshop/5_catalog-input.ipynb b/workshop/5_catalog-input.ipynb
index 481fe5d..ac4c931 100644
--- a/workshop/5_catalog-input.ipynb
+++ b/workshop/5_catalog-input.ipynb
@@ -29,7 +29,6 @@
    "source": [
     "%%writefile xpublish_spoi.py\n",
     "\n",
-    "port=9000\n",
     "ssl_keyfile=\"/work/bm0021/k204210/cloudify/workshop/key.pem\"\n",
     "ssl_certfile=\"/work/bm0021/k204210/cloudify/workshop/cert.pem\"\n",
     "\n",
@@ -43,7 +42,20 @@
     "import sys\n",
     "import os\n",
     "import intake\n",
+    "def is_port_free(port, host=\"localhost\"):\n",
+    "    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:\n",
+    "        return s.connect_ex((host, port)) != 0  # Returns True if the port is free\n",
+    "\n",
+    "def find_free_port(start=5000, end=5100, host=\"localhost\"):\n",
+    "    for port in range(start, end + 1):\n",
+    "        if is_port_free(port, host):\n",
+    "            return port\n",
+    "    return None  # No free ports found\n",
     "\n",
+    "port = find_free_port(9000,9100)\n",
+    "if not port:\n",
+    "    raise ValueError(\"Could not find a free port for service\")\n",
+    "    \n",
     "nest_asyncio.apply()\n",
     "STORAGE_OPTIONS=dict(\n",
     "    cache_size=0\n",
-- 
GitLab