May-05-2025, 09:21 AM
(May-01-2025, 07:39 PM)deanhystad Wrote: It looks like you have something you made using app inventor sending a string to a python program. The python program executes the command represented by that string. The most important detail here is that app inventor is sending a string, not a command.
You could use eval() to execute the command.
eval("glvars.cam.start_preview(True)")That is easy, but potentially dangerous. eval will execute any string it is passed, including commands to delete all files on your computer or install malware from a website. Unless you know for sure that there is no way malicious commands can be sent to your program, you should pass on eval.
Thanks, as a starting point I tried eval(). In my main .py file there is the listening socket:
recvdata = client_sock.recv(1024) stringdata = recvdata.decode('utf-8') print ("Received data:", stringdata) dataList = stringdata.split('#') target = dataList[0] cmd = dataList[1] myModules.cmdExec(target, client_sock, cmd)in glvars.py:
from picamera2 import Picamera2, Preview # Camera init cam = Picamera2() ...in myModules.py:
def cmdExec(*args): target = args[0] clientSock = args[1] cmd = args[2] if target == "0": # System commands try: res = subprocess.run([cmd], shell=True, check=True, capture_output=True) except FileNotFoundError as exc: print(f"FileNotFoundError: {exc}") clientSock.send(f"FileNotFoundError: {exc}\n") except subprocess.CalledProcessError as exc: print(f"CalledProcessError: {exc}") clientSock.send(f"CalledProcessError:\n{exc}\nScript killed\n") except subprocess.TimeoutExpired as exc: print(f"TimeoutExpired: {exc}") out = res.stdout.decode("utf-8") print('output of command:', out) clientSock.send(f"{out}\n") elif target == "1": # Camera commands camExec(clientSock, cmd) def camExec(*args): # Camera commands clientSock = args[0] cmd = args[1] print(cmd) # eval("glvars.cam.start_preview(True)") # it works eval("glvars.cam.cmd")From App Inventor I send a test text string: "1#start_preview(True)". The script exits with:
AttributeError: 'Picamera2' object has no attribute 'cmd'