Skip to content
Snippets Groups Projects

Webservice: Use ast.literal_eval() for parsing ZMQ requests

Merged Thomas Kluyver requested to merge literal-eval into master
1 unresolved thread
1 file
+ 15
16
Compare changes
  • Side-by-side
  • Inline
+ 15
16
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()
Please register or sign in to reply
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]
Loading