#!/usr/bin/env python3 import os import socket import time import sys from pathlib import Path BASE_DIR = Path(__file__).resolve().parent ENV_PATH = BASE_DIR / ".env" if ENV_PATH.exists(): for raw in ENV_PATH.read_text().splitlines(): line = raw.strip() if line and not line.startswith("#") and "=" in line: k, v = line.split("=", 1) os.environ.setdefault(k.strip(), v.strip()) host = os.environ.get("MQTT_HOST", "17.126.1.70") port = int(os.environ.get("MQTT_PORT", "1883")) deadline = time.time() + 120 while time.time() < deadline: try: with socket.create_connection((host, port), timeout=5): print(f"[OK] MQTT reachable {host}:{port}") sys.exit(0) except OSError as exc: print(f"[WAIT] MQTT {host}:{port} not reachable yet: {exc}") time.sleep(5) print(f"[ERROR] MQTT {host}:{port} not reachable after timeout") sys.exit(1)