From 9f2f163d788c3ceab1c79a60eb2b32a398970408 Mon Sep 17 00:00:00 2001 From: Frank Villaro-Dixon Date: Wed, 28 Aug 2024 00:05:41 +0200 Subject: [PATCH] add client cfg Signed-off-by: Frank Villaro-Dixon --- client.py | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/client.py b/client.py index 3426328..a34cdfb 100644 --- a/client.py +++ b/client.py @@ -2,14 +2,16 @@ import asyncio import websockets import hashlib import random +import os +import sys import conn -LOCALHOST = '127.0.0.1' -PORT = 8443 -WEBSOCKET_URL = 'ws://localhost:9999/data' +LOCALHOST = '::1' +DEFAULT_LISTEN_PORT = 6443 +WEBSOCKET_URL = 'ws://localhost:9999' async def handle_client(socket_reader, socket_writer): @@ -70,16 +72,39 @@ async def handle_client(socket_reader, socket_writer): socket_writer.close() await socket_writer.wait_closed() + + + +def get_config(): + KUBE_API_PORT = os.environ.get('KUBE_API_PORT', DEFAULT_LISTEN_PORT) + + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} ") + sys.exit(1) + + ws_url = sys.argv[1] + global WEBSOCKET_URL + # Yeah, I know this is fugly + WEBSOCKET_URL = ws_url + + return { + 'kube_api_port': KUBE_API_PORT, + 'websocket_url': ws_url, + } + + async def main(): - port = PORT - while True: - try: - server = await asyncio.start_server(handle_client, LOCALHOST, port) - break - except OSError: - port += 1 + cfg = get_config() + + try: + server = await asyncio.start_server(handle_client, LOCALHOST, cfg['kube_api_port']) + except OSError as e: + print(e) + print(f"There already is a client listening on port {cfg['kube_api_port']}") + sys.exit(1) + async with server: - print(f"Server started on {LOCALHOST}:{port}") + print(f"Server started on {LOCALHOST} port {cfg['kube_api_port']}") await server.serve_forever() if __name__ == "__main__":