Spaces:
Paused

Fffffd commited on
Commit
6c16eaa
·
verified ·
1 Parent(s): 4f24e5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -39
app.py CHANGED
@@ -1,25 +1,21 @@
 
 
1
  import subprocess
2
  import sys
3
- import os
4
- from fastapi import FastAPI, HTTPException
5
- from fastapi.responses import JSONResponse, StreamingResponse
6
- import uvicorn
7
- import asyncio
8
  import threading
9
  import queue
 
10
  import time
11
 
12
  app = FastAPI()
13
 
14
- # Istihbarat çıktısını topla
15
  output_queue = queue.Queue()
16
  process = None
 
17
 
18
  def run_istihbarat():
19
- """Istihbarat'ı arka planda çalıştır."""
20
  global process
21
-
22
- # Python dosyasını çalıştır
23
  cmd = [sys.executable, "Istihbarat.py"]
24
  process = subprocess.Popen(
25
  cmd,
@@ -31,54 +27,36 @@ def run_istihbarat():
31
  )
32
 
33
  # Çıktıyı oku
34
- while True:
35
- line = process.stdout.readline()
36
- if not line:
37
- break
38
- output_queue.put(line)
39
- print(f"[ISTI] {line.strip()}")
40
 
41
- # Istihbarat'ı başlat
42
  threading.Thread(target=run_istihbarat, daemon=True).start()
43
 
44
  @app.get("/")
45
  async def root():
46
- return {"status": "online", "message": "Dexo API Çalışıyor"}
47
 
48
  @app.get("/output")
49
  async def get_output():
50
- """Son çıktıları getir."""
51
  outputs = []
52
  while not output_queue.empty():
53
- try:
54
- outputs.append(output_queue.get_nowait())
55
- except:
56
- break
57
  return {"output": "".join(outputs)}
58
 
59
- @app.post("/command")
60
- async def send_command(command: dict):
61
- """Istihbarat'a komut gönder."""
62
- cmd = command.get("cmd", "")
63
  if not cmd:
64
  raise HTTPException(400, "cmd gerekli")
65
 
66
  if process and process.stdin:
67
  process.stdin.write(cmd + "\n")
68
  process.stdin.flush()
69
- return {"status": "sent", "command": cmd}
70
- return {"status": "error", "message": "Process çalışmıyor"}
71
-
72
- @app.get("/stream")
73
- async def stream_output():
74
- """SSE ile canlı çıktı akışı."""
75
- async def generate():
76
- while True:
77
- while not output_queue.empty():
78
- yield f"data: {output_queue.get_nowait()}\n\n"
79
- await asyncio.sleep(0.1)
80
-
81
- return StreamingResponse(generate(), media_type="text/event-stream")
82
 
83
  if __name__ == "__main__":
84
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import JSONResponse
3
  import subprocess
4
  import sys
 
 
 
 
 
5
  import threading
6
  import queue
7
+ import uvicorn
8
  import time
9
 
10
  app = FastAPI()
11
 
12
+ # Kuyruk
13
  output_queue = queue.Queue()
14
  process = None
15
+ input_queue = queue.Queue()
16
 
17
  def run_istihbarat():
 
18
  global process
 
 
19
  cmd = [sys.executable, "Istihbarat.py"]
20
  process = subprocess.Popen(
21
  cmd,
 
27
  )
28
 
29
  # Çıktıyı oku
30
+ for line in iter(process.stdout.readline, ''):
31
+ if line:
32
+ output_queue.put(line)
 
 
 
33
 
 
34
  threading.Thread(target=run_istihbarat, daemon=True).start()
35
 
36
  @app.get("/")
37
  async def root():
38
+ return {"status": "online", "message": "Dexo API"}
39
 
40
  @app.get("/output")
41
  async def get_output():
42
+ """Son çıktıları al."""
43
  outputs = []
44
  while not output_queue.empty():
45
+ outputs.append(output_queue.get_nowait())
 
 
 
46
  return {"output": "".join(outputs)}
47
 
48
+ @app.post("/cmd")
49
+ async def send_command(data: dict):
50
+ """Komut gönder."""
51
+ cmd = data.get("cmd", "")
52
  if not cmd:
53
  raise HTTPException(400, "cmd gerekli")
54
 
55
  if process and process.stdin:
56
  process.stdin.write(cmd + "\n")
57
  process.stdin.flush()
58
+ return {"status": "ok", "cmd": cmd}
59
+ return {"status": "error"}
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
  uvicorn.run(app, host="0.0.0.0", port=7860)