Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
-
|
| 4 |
-
# --- CRITICAL FIX: Set Display before importing PyAutoGUI ---
|
| 5 |
os.environ["DISPLAY"] = ":99"
|
| 6 |
|
| 7 |
import base64
|
| 8 |
-
import
|
| 9 |
-
from flask import Flask, request, jsonify
|
| 10 |
from flask_cors import CORS
|
| 11 |
import pyautogui
|
| 12 |
-
import mss
|
| 13 |
-
from PIL import Image
|
| 14 |
-
import io
|
| 15 |
|
| 16 |
app = Flask(__name__)
|
| 17 |
CORS(app)
|
|
@@ -19,31 +15,30 @@ CORS(app)
|
|
| 19 |
SCREEN_WIDTH = 1024
|
| 20 |
SCREEN_HEIGHT = 768
|
| 21 |
|
| 22 |
-
# PyAutoGUI
|
| 23 |
pyautogui.FAILSAFE = False
|
| 24 |
pyautogui.PAUSE = 0.1
|
| 25 |
|
| 26 |
-
sct = mss.mss()
|
| 27 |
-
monitor = {"top": 0, "left": 0, "width": SCREEN_WIDTH, "height": SCREEN_HEIGHT}
|
| 28 |
-
|
| 29 |
@app.route('/')
|
| 30 |
def home():
|
| 31 |
-
return "Cloud PC
|
| 32 |
|
| 33 |
@app.route('/snapshot', methods=['GET'])
|
| 34 |
def get_snapshot():
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
return jsonify({"screenshot": b64})
|
| 45 |
except Exception as e:
|
| 46 |
-
print(f"
|
|
|
|
| 47 |
return jsonify({"error": str(e)}), 500
|
| 48 |
|
| 49 |
@app.route('/interact', methods=['POST'])
|
|
@@ -62,17 +57,13 @@ def interact():
|
|
| 62 |
|
| 63 |
elif action == 'key':
|
| 64 |
k = data.get('key').lower()
|
| 65 |
-
|
| 66 |
-
elif k == 'backspace': pyautogui.press('backspace')
|
| 67 |
-
|
| 68 |
-
elif action == 'scroll':
|
| 69 |
-
pyautogui.scroll(-5)
|
| 70 |
|
| 71 |
elif action == 'open_terminal':
|
| 72 |
-
|
| 73 |
|
| 74 |
elif action == 'open_browser':
|
| 75 |
-
|
| 76 |
|
| 77 |
return jsonify({"status": "ok"})
|
| 78 |
except Exception as e:
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
+
# Set Display FIRST
|
|
|
|
| 4 |
os.environ["DISPLAY"] = ":99"
|
| 5 |
|
| 6 |
import base64
|
| 7 |
+
import subprocess
|
| 8 |
+
from flask import Flask, request, jsonify
|
| 9 |
from flask_cors import CORS
|
| 10 |
import pyautogui
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
CORS(app)
|
|
|
|
| 15 |
SCREEN_WIDTH = 1024
|
| 16 |
SCREEN_HEIGHT = 768
|
| 17 |
|
| 18 |
+
# PyAutoGUI Setup
|
| 19 |
pyautogui.FAILSAFE = False
|
| 20 |
pyautogui.PAUSE = 0.1
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.route('/')
|
| 23 |
def home():
|
| 24 |
+
return "Linux Cloud PC Running..."
|
| 25 |
|
| 26 |
@app.route('/snapshot', methods=['GET'])
|
| 27 |
def get_snapshot():
|
| 28 |
try:
|
| 29 |
+
# METHOD: Use Linux 'scrot' tool (Most stable for Docker)
|
| 30 |
+
# 1. Take screenshot to a temp file
|
| 31 |
+
subprocess.run(["scrot", "/tmp/screen.jpg", "-q", "40", "-o"], check=True)
|
| 32 |
|
| 33 |
+
# 2. Read the file
|
| 34 |
+
with open("/tmp/screen.jpg", "rb") as image_file:
|
| 35 |
+
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
|
| 36 |
+
|
| 37 |
+
return jsonify({"screenshot": encoded_string})
|
| 38 |
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
+
print(f"Screen Error: {e}")
|
| 41 |
+
# Return a dummy black image data if fails, to keep connection alive
|
| 42 |
return jsonify({"error": str(e)}), 500
|
| 43 |
|
| 44 |
@app.route('/interact', methods=['POST'])
|
|
|
|
| 57 |
|
| 58 |
elif action == 'key':
|
| 59 |
k = data.get('key').lower()
|
| 60 |
+
pyautogui.press(k)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
elif action == 'open_terminal':
|
| 63 |
+
subprocess.Popen("xfce4-terminal", shell=True)
|
| 64 |
|
| 65 |
elif action == 'open_browser':
|
| 66 |
+
subprocess.Popen("chromium-browser --no-sandbox", shell=True)
|
| 67 |
|
| 68 |
return jsonify({"status": "ok"})
|
| 69 |
except Exception as e:
|