From 566b7c71ea2607d7628c0cf7cd8891f6b5e957be Mon Sep 17 00:00:00 2001 From: minguezsanzjuanjose Date: Sun, 2 Aug 2026 14:06:40 +0200 Subject: [PATCH] 02-ago: [proyecto] vigilancia - fix: servir el mp3 con el python del sistema (regla firewall Allow) para que la tele pueda descargar el anuncio --- vigilancia/voz.py | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/vigilancia/voz.py b/vigilancia/voz.py index 08cd6f5..44468e4 100644 --- a/vigilancia/voz.py +++ b/vigilancia/voz.py @@ -1,9 +1,9 @@ import asyncio import os import socket -import threading +import subprocess +import sys import time -from http.server import HTTPServer, SimpleHTTPRequestHandler import config @@ -11,6 +11,15 @@ MEDIA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "voz_media" _server = None +def _python_firewall_ok(): + """Python del sistema (con regla de firewall Allow) para servir el mp3.""" + if hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix: + ruta = os.path.join(sys.base_prefix, "python.exe") + if os.path.exists(ruta): + return ruta + return sys.executable + + def _ip_lan(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: @@ -42,21 +51,20 @@ def sintetizar(texto, nombre="anuncio.mp3"): return None -class _Handler(SimpleHTTPRequestHandler): - def __init__(self, *a, **kw): - super().__init__(*a, directory=MEDIA_DIR, **kw) - - def log_message(self, *a): - pass - - def _arrancar_servidor(): global _server - if _server is not None: + if _server is not None and _server.poll() is None: return try: - _server = HTTPServer(("0.0.0.0", config.GOOGLE_TV_PUERTO), _Handler) - threading.Thread(target=_server.serve_forever, daemon=True).start() + py = _python_firewall_ok() + _server = subprocess.Popen( + [py, "-m", "http.server", str(config.GOOGLE_TV_PUERTO), + "--directory", MEDIA_DIR], + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0), + ) + time.sleep(1) except Exception as e: print(f"[Voz] No se pudo arrancar el servidor HTTP: {e}")