Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
import subprocess
|
| 5 |
import asyncio
|
|
|
|
| 6 |
|
| 7 |
from threading import Thread
|
| 8 |
from queue import Queue, Empty
|
|
@@ -119,3 +120,71 @@ async def websocket_endpoint(websocket: WebSocket, elo: str):
|
|
| 119 |
else:
|
| 120 |
break
|
| 121 |
await asyncio.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
import subprocess
|
| 5 |
import asyncio
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
from threading import Thread
|
| 9 |
from queue import Queue, Empty
|
|
|
|
| 120 |
else:
|
| 121 |
break
|
| 122 |
await asyncio.sleep(0.1)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
########################
|
| 129 |
+
async def get_prompt():
|
| 130 |
+
# Get current working directory
|
| 131 |
+
cwd = os.getcwd()
|
| 132 |
+
# Shorten path if it is too long
|
| 133 |
+
if len(cwd) > 30:
|
| 134 |
+
cwd = os.path.basename(cwd)
|
| 135 |
+
cwd = f".../{cwd}"
|
| 136 |
+
return f"{cwd}$ "
|
| 137 |
+
|
| 138 |
+
async def exec_command(command):
|
| 139 |
+
try:
|
| 140 |
+
# Execute command using subprocess
|
| 141 |
+
process = await asyncio.create_subprocess_shell(
|
| 142 |
+
command,
|
| 143 |
+
stdout=subprocess.PIPE,
|
| 144 |
+
stderr=subprocess.PIPE,
|
| 145 |
+
shell=True,
|
| 146 |
+
executable='/bin/bash'
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
# Wait for command to complete
|
| 150 |
+
stdout, stderr = await process.communicate()
|
| 151 |
+
|
| 152 |
+
# Prepare output to send back
|
| 153 |
+
output = stdout.decode() + stderr.decode()
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
output = str(e)
|
| 157 |
+
|
| 158 |
+
return output
|
| 159 |
+
|
| 160 |
+
async def send_prompt(websocket):
|
| 161 |
+
# Send initial prompt to client
|
| 162 |
+
prompt = await get_prompt()
|
| 163 |
+
await websocket.send(prompt)
|
| 164 |
+
|
| 165 |
+
@app.websocket("/shell")
|
| 166 |
+
async def websocket_endpoint(websocket: WebSocket):
|
| 167 |
+
await websocket.accept()
|
| 168 |
+
active_connections.add(websocket)
|
| 169 |
+
await send_prompt(websocket)
|
| 170 |
+
|
| 171 |
+
try:
|
| 172 |
+
while True:
|
| 173 |
+
# Receive command from client
|
| 174 |
+
command = await websocket.receive_text()
|
| 175 |
+
|
| 176 |
+
if command == "exit":
|
| 177 |
+
await websocket.send("Goodbye!")
|
| 178 |
+
break
|
| 179 |
+
|
| 180 |
+
# Execute command
|
| 181 |
+
output = await exec_command(command)
|
| 182 |
+
|
| 183 |
+
# Send output to client
|
| 184 |
+
await websocket.send(output)
|
| 185 |
+
|
| 186 |
+
# Send updated prompt to client
|
| 187 |
+
await send_prompt(websocket)
|
| 188 |
+
|
| 189 |
+
except websockets.exceptions.ConnectionClosedError:
|
| 190 |
+
pass
|