import asyncio import websockets import sys import json #import importlib #import pathlib import os import subprocess async def hello(websocket): #websocket is a WebSocketServerProtocol message = await websocket.recv() host=websocket.remote_address[0] #delegate parsing to subroutine #assume the message will be #pathToPythonCode:argument #where argument is some sort of a string or a number #pathToPythonCode can have replacementStrings #that reflect local software arrangement, #for example _softwareSrc_ will get replaced by #software/src directory from setup.json message=':'.join([host,message]) print(f"<<< FROM: {host}") print(f"<<< {message}") greeting = f"Hello {message}!" #run(message) #await websocket.send(greeting) #don't send anything, just log print(f">>> {greeting}") async def main(): origins=["merlin","labkey-public","onko-nix",None] #server="vangogh.fmf.uni-lj.si" server="193.2.68.227" print(f"Starting server at {server}") async with websockets.serve(hello, server, 8765,origins=origins): await asyncio.Future() # run forever def run(message): args=message.split(':') scriptFile=args[1] fsetup=os.path.join(os.path.expanduser('~'),'.labkey','setup.json') with open(fsetup) as f: setup=json.load(f) parts=scriptFile.split('/') scriptDir=parts[-2]#one before last, last is the script name scriptFile=scriptFile.replace('_softwareSrc_',setup['paths']['softwareSrc']) venv=setup['venv'][scriptDir] pCall=os.path.join(venv,'bin','python') argsCall=[pCall,scriptFile,args[0],args[2]] subprocess.run(argsCall) #spath=pathlib.Path(scriptFile) #print('script: {} [{}]{}'.format(scriptFile,spath.parent,spath.stem)) #sys.path.append(str(spath.parent)) #print(sys.path) #module=importlib.import_module(spath.stem) #module.main(args[0],args[2]) if __name__ == "__main__": asyncio.run(main())