From: Benjamin Braatz Date: Wed, 20 Jan 2021 10:10:52 +0000 (+0100) Subject: Wait for network X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=7fbd13c18ef38db42db289eba4740e3b9692c113;p=graphit%2Fschaltschrank.git Wait for network --- diff --git a/controlpi.service b/controlpi.service index 0ecd8de..535996c 100644 --- a/controlpi.service +++ b/controlpi.service @@ -1,5 +1,7 @@ [Unit] Description=Control Pi Service +Wants=network-online.target +After=network-online.target [Service] WorkingDirectory=/home/pi diff --git a/graphit_controlpi/websocket.py b/graphit_controlpi/websocket.py index 775e8dd..7ee7fee 100644 --- a/graphit_controlpi/websocket.py +++ b/graphit_controlpi/websocket.py @@ -83,22 +83,25 @@ async def process_request(server_root, path, request_headers): return HTTPStatus.OK, response_headers, body -def get_ip(): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - s.connect(('10.255.255.255', 1)) - ip = s.getsockname()[0] - except Exception: - ip = '127.0.0.1' - finally: - s.close() +async def get_ip(): + ip = None + while not ip: + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + s.connect(('10.255.255.255', 1)) + ip = s.getsockname()[0] + except Exception: + ip = None + await asyncio.sleep(0.1) + finally: + s.close() return ip async def setup_websocket(pins, queues, server_root): parameterised_handler = functools.partial(handler, pins, queues) parameterised_process_request = functools.partial(process_request, server_root) - hostname = get_ip() + hostname = await get_ip() await websockets.serve(parameterised_handler, hostname, 80, process_request=parameterised_process_request) print(f"Serving on ws://{hostname}:80")