123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import asyncio
- import websockets
- import sys
- import json
- import importlib
- import pathlib
- import os
- 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"
- 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)
- scriptFile=scriptFile.replace('_softwareSrc_',setup['paths']['softwareSrc'])
-
- 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())
|