Con un handler HTTP que también gestione WebSockets podemos gestionar los WebSockets en el mismo puerto HTTP, así no es necesario usar / abrir dos puertos. Para eso he usado el handler creado por SevenW en: https://github.com/SevenW/httpwebsockethandler y que extiende el SimpleHTTPServer. Aunque ha sido ligeralmente modificado: - He creado un wrapper para las peticiones HTTP GET, el antiguo do_GET ahora es do_GET_HTTP - He desactivado una parte de autenticación que tenía (ni he mirado en intentar mantelerlo, aunque si mantenemos el del do_GET_HTTP, pero supongo que también debería protegerse el de las querys WS y seguramente sería muy muy sencillo reactivarle esa funcionaliad y quitarle el comentario) En el server mantenemos el antiguo do_GET pero renombrado a do_GET_HTTP, y se han movido los métodos del WebSocketServer a este handler. He quitado las cosas que configuran un puerto WebSocket ya que ahora es el mismo que HTTP. Había pensado en enviarlo en otra carpetita (mediaserver-alt) o algo así, por si acaso usar lo otro tiene algún setido (como que no se usen ciertas clases por compatibilidad con pythons antiguos), pero bueno, por ahora lo dejo aquí, siempre se puede hacer rollback + crear una nueva carpeta.
99 lines
3.5 KiB
Python
99 lines
3.5 KiB
Python
#! /usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
# ------------------------------------------------------------
|
|
# Launcher
|
|
# ------------------------------------------------------------
|
|
import os
|
|
import sys
|
|
import threading
|
|
import time
|
|
from functools import wraps
|
|
|
|
sys.dont_write_bytecode = True
|
|
from platformcode import config
|
|
|
|
sys.path.append(os.path.join(config.get_runtime_path(), 'lib'))
|
|
from platformcode import platformtools, logger
|
|
import HTTPAndWSServer
|
|
|
|
http_port = config.get_setting("server.port")
|
|
websocket_port = config.get_setting("websocket.port")
|
|
myip = config.get_local_ip()
|
|
|
|
|
|
def thread_name_wrap(func):
|
|
@wraps(func)
|
|
def bar(*args, **kw):
|
|
if "name" not in kw:
|
|
kw['name'] = threading.current_thread().name
|
|
return func(*args, **kw)
|
|
|
|
return bar
|
|
|
|
|
|
threading.Thread.__init__ = thread_name_wrap(threading.Thread.__init__)
|
|
|
|
if sys.version_info < (2, 7, 11):
|
|
import ssl
|
|
|
|
ssl._create_default_https_context = ssl._create_unverified_context
|
|
|
|
|
|
def show_info():
|
|
os.system('cls' if os.name == 'nt' else 'clear')
|
|
print ("--------------------------------------------------------------------")
|
|
print ("Alfa Iniciado")
|
|
print ("La URL para acceder es http://%s:%s" % (myip, http_port))
|
|
print ("WebSocket Server iniciado en ws://%s:%s" % (myip, websocket_port))
|
|
print ("--------------------------------------------------------------------")
|
|
print ("Runtime Path : " + config.get_runtime_path())
|
|
print ("Data Path : " + config.get_data_path())
|
|
print ("Download Path : " + config.get_setting("downloadpath"))
|
|
print ("DownloadList Path : " + config.get_setting("downloadlistpath"))
|
|
print ("Bookmark Path : " + config.get_setting("bookmarkpath"))
|
|
print ("Videolibrary Path : " + config.get_setting("videolibrarypath"))
|
|
print ("--------------------------------------------------------------------")
|
|
controllers = platformtools.controllers
|
|
for a in controllers:
|
|
try:
|
|
print platformtools.controllers[a].controller.client_ip + " - (" + platformtools.controllers[
|
|
a].controller.name + ")"
|
|
except:
|
|
pass
|
|
|
|
|
|
def start():
|
|
logger.info("server init...")
|
|
config.verify_directories_created()
|
|
try:
|
|
HTTPAndWSServer.start(show_info)
|
|
|
|
# Da por levantado el servicio
|
|
logger.info("--------------------------------------------------------------------")
|
|
logger.info("Alfa Iniciado")
|
|
logger.info("La URL para acceder es http://%s:%s" % (myip, http_port))
|
|
logger.info("WebSocket Server iniciado en ws://%s:%s" % (myip, websocket_port))
|
|
logger.info("--------------------------------------------------------------------")
|
|
logger.info("Runtime Path : " + config.get_runtime_path())
|
|
logger.info("Data Path : " + config.get_data_path())
|
|
logger.info("Download Path : " + config.get_setting("downloadpath"))
|
|
logger.info("DownloadList Path : " + config.get_setting("downloadlistpath"))
|
|
logger.info("Bookmark Path : " + config.get_setting("bookmarkpath"))
|
|
logger.info("VideoLibrary Path : " + config.get_setting("videolibrarypath"))
|
|
logger.info("--------------------------------------------------------------------")
|
|
show_info()
|
|
|
|
flag = True
|
|
while flag:
|
|
time.sleep(1)
|
|
|
|
except KeyboardInterrupt:
|
|
print 'Deteniendo el servidor HTTP...'
|
|
HTTPAndWSServer.stop()
|
|
print 'Alfa Detenido'
|
|
flag = False
|
|
|
|
|
|
# Inicia el programa
|
|
start()
|