diff --git a/webservice/webservice.py b/webservice/webservice.py index 0d1696e53e29fb83971c47cf4784062198834d93..1563807952ec0923922c3e43a378b26abf245d61 100644 --- a/webservice/webservice.py +++ b/webservice/webservice.py @@ -1,4 +1,5 @@ import argparse +import ast import asyncio import copy import getpass @@ -669,15 +670,13 @@ class ActionsServer: The main server loop handles remote requests via a ZMQ interface. - Requests are the form of ZMQ.REQuest and have the format - - command, *params - - where *parms is a string-encoded python list as defined by the - commands. + Requests are serialised as the repr of a Python list/tuple, with the + first element identifying the action, and subsequent elements arguments + to be passed to that action. The number & meaning of arguments depends + on the action. """ while True: - req = await self.socket.recv_multipart() + req = await self.socket.recv() logging.debug("Raw request data: %r", req) try: resp = await self.handle_one_req(req) @@ -688,13 +687,13 @@ class ActionsServer: logging.debug("Sending response: %r", resp) await self.socket.send(resp) - async def handle_one_req(self, req: List[bytes]) -> bytes: - if len(req) == 1: - try: # protect against unparseable requests - req = eval(req[0]) - except SyntaxError as e: - logging.error(str(e)) - return Errors.REQUEST_FAILED.encode() + async def handle_one_req(self, raw_req: bytes) -> bytes: + """Handle one request, and return the reply to be sent""" + try: # protect against unparseable requests + req = ast.literal_eval(raw_req.decode('utf-8')) + except SyntaxError as e: + logging.error(str(e)) + return Errors.REQUEST_FAILED.encode() if len(req) < 2: # catch parseable but malformed requests logging.error(Errors.REQUEST_MALFORMED.format(req)) @@ -860,7 +859,7 @@ class ActionsServer: karabo_das = karabo_das.split(',') for i, run in enumerate(runs): - erun = eval(run) + erun = ast.literal_eval(run) if isinstance(erun, (list, tuple)): typ, runnr = erun if typ == "reservation": @@ -983,7 +982,7 @@ class ActionsServer: """ request_time = datetime.now().strftime('%Y-%m-%dT%H:%M:%S') try: - pdus, karabo_das, wait_runs = eval(','.join(extra)) + pdus, karabo_das, wait_runs = ast.literal_eval(','.join(extra)) karabo_das = [val.strip() for val in karabo_das] runs = [str(val) for val in wait_runs]