This repository provides a FastAPI-based service that enables remote execution of Python scripts with optional dependency installation. Users can upload their Python files and execute main.py
in a sandboxed environment.
- Remote Code Execution: Upload and execute Python scripts in a controlled environment.
- Dependency Management: Automatically install specified Python dependencies before execution.
- File-Based Input/Output: Supports file uploads and returns execution results, including standard output, errors, and generated files.
- Security: Runs in an isolated
/tmp
directory with automatic cleanup after execution. - Multipart Response: Returns execution results and output files in a multipart response format.
Deploy your own instance of this executor using Genezio:
- Method:
POST
- Description: Accepts Python files and executes
main.py
in an isolated environment.
Parameter | Type | Description |
---|---|---|
dependencies |
string (query) |
Comma-separated list of Python packages to install. Default is empty. |
files |
multipart/form-data |
One or more Python files (including main.py ). |
curl -X POST "http://localhost:8000/execute?dependencies=requests,numpy" \
-F "[email protected]" \
-F "[email protected]"
The response is a multipart/form-data
containing:
- stdout: Standard output of the script.
- stderr: Standard error output.
- Generated Files (if any) from the script execution.
{
"stdout": "Execution completed successfully.\n",
"stderr": "",
"output.txt": "This is the generated file content."
}
-
Dependency Installation
- Dependencies are parsed from the query string and installed using
pip
into/tmp/deps
. - The
PYTHONPATH
is updated to include/tmp/deps
before executing the script.
- Dependencies are parsed from the query string and installed using
-
File Handling
- Uploaded files are stored in
/tmp/code_exec
. - The script
main.py
must be present, or execution will fail.
- Uploaded files are stored in
-
Execution Flow
- Uploaded files are saved.
- Dependencies (if any) are installed.
main.py
is executed within the/tmp/code_exec
directory.- The output is captured and returned in a multipart response.
- The
/tmp/code_exec
directory is cleaned up after execution.
- The execution environment is cleaned after every request.
- Runs in an isolated directory to prevent conflicts.
- Ensure trusted code execution to avoid malicious scripts.
This project is licensed under the MIT License.