import asyncio import traceback from websockets.server import serve from yapic import json class Test: def hello(self, text): return f'hello {text}' services = { 'test': Test(), } async def handler(socket): async for message in socket: msg = json.loads(message) err = None match msg['cmd']: case 'list': data = list(services.keys()) case 'call': try: data = getattr(services[msg['name']], msg['prop'])(*msg['args']) except Exception as _err: print(traceback.format_exc()) err = str(_err) await socket.send(json.dumps({ 'id': msg['id'], 'err': err, 'data': data, })) async def main(): async with serve(handler, '127.0.0.1', 3021): await asyncio.Future() asyncio.run(main())