Spaces:
Sleeping
Sleeping
Ramesh-vani
commited on
Commit
•
cceba77
1
Parent(s):
b45eccf
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import socket
|
2 |
+
from http.server import HTTPServer, BaseHTTPRequestHandler
|
3 |
+
import subprocess
|
4 |
+
import re
|
5 |
+
import threading
|
6 |
+
|
7 |
+
class MyHandler(BaseHTTPRequestHandler):
|
8 |
+
def do_POST(self):
|
9 |
+
if self.path == '/run_code':
|
10 |
+
# Read the POST data containing the code to run
|
11 |
+
content_length = int(self.headers['Content-Length'])
|
12 |
+
code_to_run = self.rfile.read(content_length).decode('utf-8')
|
13 |
+
|
14 |
+
# Create a socket for communication with the client
|
15 |
+
client_socket, address = self.server.client_listener.accept()
|
16 |
+
|
17 |
+
# Run the code
|
18 |
+
def run_process():
|
19 |
+
try:
|
20 |
+
process = subprocess.Popen(
|
21 |
+
['python', '-c', code_to_run],
|
22 |
+
stdin=subprocess.PIPE,
|
23 |
+
stdout=subprocess.PIPE,
|
24 |
+
stderr=subprocess.PIPE,
|
25 |
+
text=True,
|
26 |
+
bufsize=1, # Line buffered
|
27 |
+
universal_newlines=True,
|
28 |
+
shell=True # Allow running shell commands
|
29 |
+
)
|
30 |
+
|
31 |
+
while True:
|
32 |
+
line = process.stdout.readline()
|
33 |
+
if not line:
|
34 |
+
break
|
35 |
+
|
36 |
+
# Send SSE event for each line in the output
|
37 |
+
client_socket.send(f'data: {line.strip()}\n\n'.encode('utf-8'))
|
38 |
+
|
39 |
+
# If 'event: input' is received, wait for input from the client
|
40 |
+
if 'event: input' in line:
|
41 |
+
user_input = client_socket.recv(1024).decode('utf-8')
|
42 |
+
process.stdin.write(user_input + '\n')
|
43 |
+
process.stdin.flush()
|
44 |
+
|
45 |
+
process.communicate()
|
46 |
+
|
47 |
+
if process.returncode == 0:
|
48 |
+
client_socket.send(b'data: Code executed successfully\n\n')
|
49 |
+
else:
|
50 |
+
error_message = process.stderr.read()
|
51 |
+
client_socket.send(f'data: Error: {error_message}\n\n'.encode('utf-8'))
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
client_socket.send(f'data: Error: {str(e)}\n\n'.encode('utf-8'))
|
55 |
+
|
56 |
+
# Send a specific SSE message to indicate the end of the process
|
57 |
+
client_socket.send(b'event: end\ndata: Process complete\n\n')
|
58 |
+
|
59 |
+
# Close the connection explicitly
|
60 |
+
client_socket.close()
|
61 |
+
|
62 |
+
threading.Thread(target=run_process).start()
|
63 |
+
|
64 |
+
self.send_response(200)
|
65 |
+
self.send_header('Content-type', 'text/event-stream')
|
66 |
+
self.send_header('Cache-Control', 'no-cache')
|
67 |
+
self.send_header('Connection', 'keep-alive')
|
68 |
+
self.end_headers()
|
69 |
+
|
70 |
+
else:
|
71 |
+
self.send_response(404)
|
72 |
+
self.send_header('Content-type', 'text/html')
|
73 |
+
self.end_headers()
|
74 |
+
self.wfile.write(b'404: Not Found')
|
75 |
+
|
76 |
+
class MyServer(HTTPServer):
|
77 |
+
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
|
78 |
+
super().__init__(server_address, RequestHandlerClass, bind_and_activate)
|
79 |
+
self.client_listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
80 |
+
self.client_listener.bind(('127.0.0.1', 7861))
|
81 |
+
self.client_listener.listen(1)
|
82 |
+
|
83 |
+
# Create an HTTP server and bind it to a specific host and port
|
84 |
+
httpd = MyServer(('0.0.0.0', 7860), MyHandler)
|
85 |
+
|
86 |
+
# Start serving indefinitely
|
87 |
+
httpd.serve_forever()
|