From 7b04fd3798109433d6f750c7d9f9b1067d18f9e7 Mon Sep 17 00:00:00 2001 From: Benjamin Braatz Date: Tue, 27 Jul 2021 08:16:48 +0200 Subject: [PATCH] Wait for port if address in use. --- controlpi_plugins/wsserver.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/controlpi_plugins/wsserver.py b/controlpi_plugins/wsserver.py index 0114f8c..a3706db 100644 --- a/controlpi_plugins/wsserver.py +++ b/controlpi_plugins/wsserver.py @@ -6,9 +6,10 @@ TODO: documentation, doctests TODO: Let Debug web app collapse/expand nested structures TODO: Make Debug web app work with nested structures in commands """ +import asyncio +import json import os import sys -import json from websockets import WebSocketServerProtocol, ConnectionClosed, serve from websockets.http import Headers from http import HTTPStatus @@ -202,6 +203,12 @@ class WSServer(BasePlugin): async def run(self) -> None: """Set up websocket server.""" - await serve(self._handler, host=self._host, port=self._port, - process_request=self._process_request) + serving = False + while not serving: + try: + await serve(self._handler, host=self._host, port=self._port, + process_request=self._process_request) + serving = True + except OSError: + await asyncio.sleep(1) print(f"WSServer '{self.name}' serving on port {self._port}.") -- 2.34.1