From 194c63e4d11e6de9792a736a7b99788f1db78741 Mon Sep 17 00:00:00 2001 From: historia Date: Mon, 24 Aug 2026 00:41:52 -0400 Subject: feat: running process detection, menu gating --- backends/common.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'backends/common.py') diff --git a/backends/common.py b/backends/common.py index 2c6437f..d707fdc 100644 --- a/backends/common.py +++ b/backends/common.py @@ -141,6 +141,31 @@ def url_with_port(url: str, port: int) -> str: (parts.scheme or "http", f"{host}:{port}", parts.path, "", "")) +def server_running(url: str, timeout: float = 0.3) -> bool: + """True when something accepts TCP connections at URL's host:port. + + A protocol-agnostic socket connect: an HTTP TTS server that is up will + accept the connection (we do not need to speak HTTP to know it is + listening). Returns False on any parse or connection error, so a + misconfigured URL never blocks the hub — it just reports the backend + as not running. Used by each backend's ``detect()`` to set + ``BackendStatus.running``. + """ + import socket + try: + parts = urllib.parse.urlsplit(url) + host = parts.hostname or "127.0.0.1" + port = parts.port or (443 if (parts.scheme or "http") == "https" + else 80) + except ValueError: + return False + try: + with socket.create_connection((host, port), timeout=timeout): + return True + except OSError: + return False + + def update_config_value(key: str, value: str, config_path: Optional[Path] = None) -> bool: """Rewrite a ``KEY = "value"`` line in converter/config.py. -- cgit v1.2.3