02-ago: [proyecto] vigilancia - fix: servir el mp3 con el python del sistema (regla firewall Allow) para que la tele pueda descargar el anuncio
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import subprocess
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
|
||||||
|
|
||||||
import config
|
import config
|
||||||
|
|
||||||
@@ -11,6 +11,15 @@ MEDIA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "voz_media"
|
|||||||
_server = None
|
_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():
|
def _ip_lan():
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
try:
|
try:
|
||||||
@@ -42,21 +51,20 @@ def sintetizar(texto, nombre="anuncio.mp3"):
|
|||||||
return None
|
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():
|
def _arrancar_servidor():
|
||||||
global _server
|
global _server
|
||||||
if _server is not None:
|
if _server is not None and _server.poll() is None:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
_server = HTTPServer(("0.0.0.0", config.GOOGLE_TV_PUERTO), _Handler)
|
py = _python_firewall_ok()
|
||||||
threading.Thread(target=_server.serve_forever, daemon=True).start()
|
_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:
|
except Exception as e:
|
||||||
print(f"[Voz] No se pudo arrancar el servidor HTTP: {e}")
|
print(f"[Voz] No se pudo arrancar el servidor HTTP: {e}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user