import websockets import asyncio import pathlib import logging from objects import glob from handlers.mainHandler import handle, DiscoveryProtocol enable_ssl = len(glob.config["ws"]["cert"]) != 0 ws_addr = (glob.config["ws"]["ip"], glob.config["ws"]["port"]) sock_addr = (glob.config["socket"]["ip"], glob.config["socket"]["port"]) ws_kwargs = {} if enable_ssl: import ssl logging.debug("Loading cert") ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_cert_chain(glob.config["ws"]["cert"], keyfile = glob.config["ws"]["key"]) ws_kwargs["ssl"] = ssl_context else: logging.debug("Skipping ssl (disabled)") logging.debug("Create websocket instance") srvr = websockets.serve( handle, *ws_addr, **ws_kwargs ) loop = asyncio.get_event_loop() logging.debug("Start main loop") logging.info("Starting websocket on %s:%d (SSL: %s)" % (*ws_addr, enable_ssl and "ENABLED" or "DISABLED")) try: #asyncio.ensure_future(wait()) loop.run_until_complete(loop.create_datagram_endpoint(DiscoveryProtocol, local_addr = sock_addr)) loop.run_until_complete(srvr) loop.run_forever() except KeyboardInterrupt: print("Exit") logging.info("Stopped")