add client cfg
Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
parent
d940d2897c
commit
9f2f163d78
1 changed files with 36 additions and 11 deletions
45
client.py
45
client.py
|
@ -2,14 +2,16 @@ import asyncio
|
||||||
import websockets
|
import websockets
|
||||||
import hashlib
|
import hashlib
|
||||||
import random
|
import random
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
import conn
|
import conn
|
||||||
|
|
||||||
|
|
||||||
LOCALHOST = '127.0.0.1'
|
LOCALHOST = '::1'
|
||||||
PORT = 8443
|
DEFAULT_LISTEN_PORT = 6443
|
||||||
WEBSOCKET_URL = 'ws://localhost:9999/data'
|
WEBSOCKET_URL = 'ws://localhost:9999'
|
||||||
|
|
||||||
|
|
||||||
async def handle_client(socket_reader, socket_writer):
|
async def handle_client(socket_reader, socket_writer):
|
||||||
|
@ -70,16 +72,39 @@ async def handle_client(socket_reader, socket_writer):
|
||||||
socket_writer.close()
|
socket_writer.close()
|
||||||
await socket_writer.wait_closed()
|
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]} <websocket_url>")
|
||||||
|
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():
|
async def main():
|
||||||
port = PORT
|
cfg = get_config()
|
||||||
while True:
|
|
||||||
try:
|
try:
|
||||||
server = await asyncio.start_server(handle_client, LOCALHOST, port)
|
server = await asyncio.start_server(handle_client, LOCALHOST, cfg['kube_api_port'])
|
||||||
break
|
except OSError as e:
|
||||||
except OSError:
|
print(e)
|
||||||
port += 1
|
print(f"There already is a client listening on port {cfg['kube_api_port']}")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
async with server:
|
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()
|
await server.serve_forever()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue