-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsandbox.py
More file actions
34 lines (28 loc) · 776 Bytes
/
sandbox.py
File metadata and controls
34 lines (28 loc) · 776 Bytes
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
import subprocess
import uuid
import os
def run_in_docker(code):
file_id = uuid.uuid4().hex
file_name = f"temp_{file_id}.py"
with open(file_name, "w") as f:
f.write(code)
try:
result = subprocess.run(
[
"docker", "run",
"--rm",
"-v", f"{os.getcwd()}:/app",
"-w", "/app",
"python:3.10-slim",
"python", file_name
],
capture_output=True,
text=True,
timeout=3
)
return result.stdout.strip()
except subprocess.TimeoutExpired:
return "Timeout"
finally:
if os.path.exists(file_name):
os.remove(file_name)