This project provides a secure web-based Python execution server with sandboxing, memory limits, execution timeouts, and persistent sessions.
👉 Cross-platform: Works on both Linux & Windows
👉 Session Management: Maintain variables across executions
👉 Security Features: Blocks dangerous imports (os, sys, subprocess, etc.)
👉 Execution Limits:
- Max Memory: 100MB per session
- Max Execution Time: 2 seconds
👉 Web UI: User-friendly interface for running code
Ensure you have Python 3.8+ installed.
📌 clone the repo:
git clone https://github.com/ah0048/Python-Code-Execution-Server.git
cd Python-Code-Execution-Server📌 create a virtual environment:
- On Linux/macOS:
python3 -m venv venv- On Windows:
python -m venv venv📌 activate the virtual environment:
- On Linux/macOS:
source venv/bin/activate- On Windows:
venv\Scripts\activate📌 Install dependencies:
pip install -r requirements.txtpython3 run.pypython run.pyThe server will start at:
📌 http://127.0.0.1:5000/
1️⃣ Open your browser and go to http://127.0.0.1:5000/
2️⃣ Write Python code in the editor
3️⃣ Click "Execute" to run it
4️⃣ (Optional) Enter a Session ID to persist variables across executions
📌 Endpoint:
POST /execute📌 Request Body (JSON):
{
"code": "print('Hello, World!')",
"id": "optional-session-id"
}📌 Response Example:
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"stdout": "Hello, World!\n"
}If an error occurs, the response will include an "error" field instead of "stdout" or "stderr".
Example (Timeout Exceeded):
{
"id": "123e4567-e89b-12d3-a456-426614174000",
"error": "Execution timeout. Session terminated."
}This server restricts unsafe operations to prevent security risks.
The following dangerous modules are blocked:
🚫 os, sys, socket, subprocess, shutil, pathlib, open
Example (Blocked Code):
import os
os.system("rm -rf /") # ❌ Not Allowed!📌 Response:
{
"stderr": "Traceback (most recent call last):\nPermissionError: Importing 'os' is restricted.\n"
}| Limit | Value |
|---|---|
| Max Execution Time | 2 seconds |
| Max Memory Usage | 100MB |
Example (Infinite Loop):
while True: pass📌 Response:
{
"error": "Execution timeout. Session terminated."
}Example (Exceeding Memory Limit):
a = " " * 1024 * 1024 * 200 # Allocate 200MB📌 Response:
{
"error": "Memory limit exceeded. Session terminated."
}🔹 File System & Network Access Blocked
🔹 No Support for Asynchronous Execution (async functions may not work properly)
🔹 Cannot Execute Multi-Threaded Code (threading is blocked)
- Python Flask Server for executing code securely
- Memory & Execution Time Limits enforced
- Persistent Sessions to store variables across executions
- Web UI for easy testing
- Works on both Windows & Linux
💡 Future Improvements:
- Add Docker support for stricter sandboxing
- Implement rate limiting to prevent abuse
- Support async execution and classes build