import json import requests class PythonController: def __init__(self, http_server: str, pkgs_prefix: str = "py -c \"import pyautogui; {command}\""): self.http_server = http_server self.pkgs_prefix = pkgs_prefix # fixme: this is a hacky way to execute python commands. fix it and combine it with installation of packages def get_screenshot(self): """ Gets a screenshot from the server. With the cursor. """ response = requests.get(self.http_server + "/screenshot") if response.status_code == 200: return response.content else: print("Failed to get screenshot. Status code:", response.status_code) return None def execute_python_command(self, command: str) -> None: """ Executes a python command on the server. It can be used to execute the pyautogui commands, or... any other python command. who knows? """ command = self.pkgs_prefix.format(command=command) payload = json.dumps({"command": command}) headers = { 'Content-Type': 'application/json' } try: response = requests.post(self.http_server + "/execute", headers=headers, data=payload) if response.status_code == 200: print("Command executed successfully:", response.text) else: print("Failed to execute command. Status code:", response.status_code) return response.json() except requests.exceptions.RequestException as e: print("An error occurred while trying to execute the command:", e)