File size: 1,464 Bytes
042d8bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# peer_server.py

from flask import Flask, request, jsonify  # استيراد request و jsonify مع Flask
import psutil
import smart_tasks
import time
import socket
from peer_discovery import PORT
from flask import Flask
app = Flask(__name__)
@app.route("/run", methods=["POST"])
def run_task():
    try:
        data = request.get_json()
        print("🔹 البيانات المستلمة:", data)
        count = data.get("count", 100)
        ...
        return jsonify(result)
    except Exception as e:
        print("❌ خطأ أثناء التنفيذ:", e)
        return jsonify({"error": str(e)}), 500
app = Flask(__name__)  # إنشاء التطبيق

@app.route("/cpu")
def cpu():
    # يعيد نسبة استخدام المعالج
    return jsonify(usage=psutil.cpu_percent(interval=0.3))

@app.route("/run", methods=["POST"])
def run():
    data = request.get_json(force=True)
    fn_name = data.get("func")
    fn = getattr(smart_tasks, fn_name, None)
    if not fn:
        return jsonify(error="function-not-found"), 404
    try:
        start = time.time()
        result = fn(*data.get("args", []), **data.get("kwargs", {}))
        return jsonify(
            result=result,
            host=socket.gethostname(),
            took=round(time.time() - start, 3)
        )
    except Exception as e:
        return jsonify(error=str(e)), 500

if __name__ == "__main__":  # التصحيح هنا
    app.run(host="0.0.0.0", port=PORT)