-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_program.py
More file actions
108 lines (85 loc) · 3.03 KB
/
Copy pathrun_program.py
File metadata and controls
108 lines (85 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Punto de entrada para ejecutar el programa en modo desarrollo (localhost).
Uso:
python run_program.py
Esto levanta Flask en http://127.0.0.1:8000 y abre el navegador
automáticamente cuando el servidor esté listo.
Para la app de escritorio (sin navegador), ejecuta PythonDSProgram.exe.
"""
from __future__ import annotations
import os
import socket
import sys
import threading
import time
import urllib.request
import webbrowser
HOST = os.getenv("PROGRAM_HOST", "127.0.0.1")
PORT = int(os.getenv("PROGRAM_PORT", "8000"))
STARTUP_TIMEOUT = 30
def _port_in_use(host: str, port: int) -> bool:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1)
try:
s.connect((host, port))
return True
except (ConnectionRefusedError, socket.timeout, OSError):
return False
def _wait_for_server(url: str, timeout: int) -> bool:
deadline = time.time() + timeout
while time.time() < deadline:
try:
with urllib.request.urlopen(url, timeout=2) as r: # nosec B310 — URL is always http://127.0.0.1
if r.status == 200:
return True
except Exception: # nosec B110 — intentional: server not ready yet, keep polling
pass
time.sleep(0.4)
return False
def main() -> None:
url = f"http://{HOST}:{PORT}"
print()
print("=" * 58)
print(" PYTHON DS PROGRAM — Modo desarrollo (localhost)")
print("=" * 58)
print(f" URL: {url}")
print()
if _port_in_use(HOST, PORT):
print(f" Ya hay un servidor en {url}. Abriendo navegador...")
webbrowser.open(url)
return
# Importar la app aquí para detectar errores antes de iniciar el hilo
try:
from app.app import app
except ImportError as exc:
print(f" ERROR: No se pudo importar la app Flask: {exc}")
print(" Asegúrate de tener instaladas las dependencias:")
print(" pip install -r requirements.txt")
sys.exit(1)
print(" Iniciando Flask en segundo plano...")
flask_thread = threading.Thread(
target=lambda: app.run(host=HOST, port=PORT, debug=False, use_reloader=False),
daemon=True,
name="flask-dev",
)
flask_thread.start()
print(f" Esperando que el servidor responda en {url}/health ...")
ready = _wait_for_server(f"{url}/health", STARTUP_TIMEOUT)
if not ready:
print()
print(" ERROR: El servidor no respondió en el tiempo esperado.")
print(" Posibles causas:")
print(f" - Puerto {PORT} bloqueado por firewall")
print(" - Error en la importación de dependencias (revisa arriba)")
print(f" Intenta abrir manualmente: {url}")
sys.exit(1)
print(" Servidor listo. Abriendo navegador...")
webbrowser.open(url)
print()
print(" Presiona Ctrl+C para detener el servidor.")
print()
try:
flask_thread.join()
except KeyboardInterrupt:
print("\n Servidor detenido.")
if __name__ == "__main__":
main()