-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.py
46 lines (38 loc) · 1.13 KB
/
server.py
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
import socket
import sys
import traceback
import io
exec_globals = {}
exec_locals = {}
def execute_code(code):
stdout = io.StringIO()
stderr = io.StringIO()
sys.stdout = stdout
sys.stderr = stderr
try:
exec(code, exec_globals, exec_locals)
except Exception as e:
traceback.print_exc(file=stderr)
output = stdout.getvalue()
error = stderr.getvalue()
# Restore original stdout and stderr
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
return output + error
def start_server(host='0.0.0.0', port=65432):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((host, port))
s.listen()
print(f"Server listening on {host}:{port}")
while True:
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
data = conn.recv(1024)
if not data:
break
code = data.decode('utf-8')
output = execute_code(code)
conn.sendall(output.encode('utf-8'))
if __name__ == "__main__":
start_server()