ip addr displayed here too + ping

This commit is contained in:
lda committed 2026-02-26 18:53:07 +07:00
1 parent d5c535a5d5
commit d6ed244bea
2 files changed
+35 -1

No files matched your search

+34
View File
@@ -15,6 +15,7 @@ Usage:
import argparse
import logging
import os
import socket
import threading
import time
from contextlib import asynccontextmanager
@@ -61,6 +62,22 @@ class LiveFrameSink(Sink):
pass
def _get_lan_address() -> tuple[str, str]:
"""Return (ip, hostname) for the primary LAN interface."""
ip = "127.0.0.1"
try:
# Open a UDP socket to an external IP (doesn't actually send anything)
# to figure out which local NIC the OS would route through.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
except Exception:
pass
hostname = socket.getfqdn()
return ip, hostname
# ---------------------------------------------------------------------------
# Pipeline runner (background thread)
# ---------------------------------------------------------------------------
@@ -185,6 +202,17 @@ async def app_lifespan(_app: FastAPI):
t.start()
logger.info("Pipeline running in background.")
# Show real addresses
ip, hostname = _get_lan_address()
port = _args.port if hasattr(_args, "port") else 8000
logger.info("=" * 50)
logger.info(" Server ready")
logger.info(" Local: http://127.0.0.1:%d", port)
logger.info(" Network: http://%s:%d", ip, port)
if hostname and hostname != ip:
logger.info(" Hostname: http://%s:%d", hostname, port)
logger.info("=" * 50)
yield # --- app serves requests ---
# Shutdown
@@ -209,6 +237,12 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
# -- Auth ------------------------------------------------------------------
@app.get("/ping")
def ping():
"""Health check for the mobile app to verify connectivity."""
return {"status": "ok", "name": "SmokingDetection"}
@app.post("/register")
def register(username: str = Form(...), password: str = Form(...)):
try: