This project is an educational exploration and practical comparison of building HTTP servers in two different programming languages: Python and Rust. The goal is to understand the trade-offs, performance characteristics, and development experience of each approach by implementing functionally equivalent servers.
- Language Comparison: Understand the practical differences between Python and Rust for web server development
- Performance Analysis: Measure and compare the performance characteristics of both implementations
- Development Experience: Experience the different development workflows, from rapid Python prototyping to Rust's compile-time guarantees
- Translation Patterns: Learn how to translate common web server patterns from Python to Rust
- Interoperability: Explore options for integrating Python and Rust code
- Prototyping to Production: Start with Python for rapid development, then migrate performance-critical parts to Rust
- Microservices Architecture: Use the right tool for each service (Python for ML/data, Rust for high-throughput APIs)
- Performance Optimization: Identify bottlenecks in Python and rewrite them in Rust
- Team Skills: Enable teams to work with both languages based on their strengths
Servers/
βββ README.md # This file
βββ main.py # Server orchestration and testing
βββ server.py # Python HTTP server implementation
βββ rust_server/ # Rust server project
β βββ Cargo.toml # Rust dependencies
β βββ src/
β β βββ main.rs # Rust HTTP server implementation
β βββ README.md # Rust-specific documentation
βββ docs/
βββ python_to_rust_translation.md # Translation guide
βββ rust_server_instructions.md # Setup instructions
For Python Server:
- Python 3.7+ (built-in modules only, no external dependencies)
For Rust Server:
- Rust 1.70+ (Install Rust)
- Cargo (comes with Rust)
For Testing:
pip install requestspython main.py pythonpython main.py bothpython main.py compareBoth servers implement the same REST API with identical endpoints:
API Endpoints
| Method | Endpoint | Description | Response |
|--------|----------------|--------------------------------|-----------------------------------|
| GET | `/` | Root endpoint | Plain text welcome message |
| GET | `/health` | Health check | JSON status object |
| POST | `/api/create` | Create a resource | JSON with created resource info |
curl http://localhost:8000/health # Python
curl http://localhost:3000/health # Rustcurl -X POST http://localhost:8000/api/create \
-H "Content-Type: application/json" \
-d '{"name": "test"}'This project is an educational exploration and practical comparison of building HTTP servers in two different programming languages: Python and Rust. The goal is to understand the trade-offs, performance characteristics, and development experience of each approach by implementing functionally equivalent servers.
- Language Comparison: Understand the practical differences between Python and Rust for web server development
- Performance Analysis: Measure and compare the performance characteristics of both implementations
- Development Experience: Experience the different development workflows, from rapid Python prototyping to Rust's compile-time guarantees
- Translation Patterns: Learn how to translate common web server patterns from Python to Rust
- Interoperability: Explore options for integrating Python and Rust code
- Prototyping to Production: Start with Python for rapid development, then migrate performance-critical parts to Rust
- Microservices Architecture: Use the right tool for each service (Python for ML/data, Rust for high-throughput APIs)
- Performance Optimization: Identify bottlenecks in Python and rewrite them in Rust
- Team Skills: Enable teams to work with both languages based on their strengths
Servers/
βββ README.md # This file
βββ main.py # Server orchestration and testing
βββ server.py # Python HTTP server implementation
βββ rust_server/ # Rust server project
β βββ Cargo.toml # Rust dependencies
β βββ src/
β β βββ main.rs # Rust HTTP server implementation
β βββ README.md # Rust-specific documentation
βββ docs/
βββ python_to_rust_translation.md # Translation guide
βββ rust_server_instructions.md # Setup instructions
For Python Server:
- Python 3.7+ (built-in modules only, no external dependencies)
For Rust Server:
- Rust 1.70+ (Install Rust)
- Cargo (comes with Rust)
For Testing:
pip install requestsStart Python Server (Port 8000):
python main.py pythonStart Rust Server (Port 3000):
python main.py rustStart Both Servers Simultaneously:
python main.py bothRun A/B Comparison Tests:
python main.py compareBoth servers implement the same REST API with identical endpoints:
| Method | Endpoint | Description | Response |
|---|---|---|---|
| GET | / |
Root endpoint | Plain text welcome message |
| GET | /health |
Health check | JSON status object |
| POST | /api/create |
Create a resource | JSON with created resource info |
Health Check:
curl http://localhost:8000/health # Python
curl http://localhost:3000/health # RustCreate Resource:
curl -X POST http://localhost:8000/api/create \
-H "Content-Type: application/json" \
-d '{"name": "test"}'| Aspect | Details |
|---|---|
| Framework | Built-in http.server module |
| Concurrency | Threading (ThreadingHTTPServer) |
| Port | 8000 |
| Startup Time | ~Instant |
| Memory Usage | ~20-30 MB |
| Development | Rapid, dynamic typing |
| Dependencies | None (standard library only) |
| Best For | Prototyping, scripting, data processing |
Pros:
- β Extremely fast to develop and iterate
- β No compilation step
- β Easy to debug and modify
- β Rich ecosystem for data science and ML
- β Readable, concise code
Cons:
β οΈ Slower runtime performanceβ οΈ GIL limits true parallelismβ οΈ Higher memory consumptionβ οΈ Runtime errors (no compile-time checks)
| Aspect | Details |
|---|---|
| Framework | Axum (modern async web framework) |
| Concurrency | Async/await with Tokio runtime |
| Port | 3000 |
| Startup Time | ~2 seconds (compilation + runtime) |
| Memory Usage | ~5-10 MB |
| Development | Compile-time safety, static typing |
| Dependencies | Axum, Tokio, Serde |
| Best For | High-performance APIs, system programming |
Pros:
- β Excellent runtime performance (10-100x faster)
- β Memory safe without garbage collection
- β True async concurrency (no GIL)
- β Compile-time error catching
- β Low memory footprint
Cons:
β οΈ Longer compilation timeβ οΈ Steeper learning curveβ οΈ More verbose codeβ οΈ Slower development iteration
The main.py script provides comprehensive testing:
# Test Python server
python main.py test-python
# Test Rust server
python main.py test-rust
# Compare both servers
python main.py comparePython Server:
# Terminal 1
python server.py
# Terminal 2
curl http://localhost:8000/healthRust Server:
# Terminal 1
cd rust_server && cargo run
# Terminal 2
curl http://localhost:3000/healthUse tools like wrk or ab (Apache Bench) for load testing:
# Install wrk (macOS)
brew install wrk
# Benchmark Python server
wrk -t4 -c100 -d30s http://localhost:8000/health
# Benchmark Rust server
wrk -t4 -c100 -d30s http://localhost:3000/health1. Request Handling
Python:
def do_GET(self):
if self.path == '/health':
response = {'status': 'ok'}
self.wfile.write(json.dumps(response).encode())Rust:
async fn health() -> Json<HealthResponse> {
Json(HealthResponse {
status: "ok".to_string(),
})
}2. Type Safety
Python: Dynamic typing, runtime errors
def create(name): # name could be anything
return {'id': 1, 'name': name}Rust: Static typing, compile-time checks
#[derive(Deserialize)]
struct CreateRequest {
name: String, // Must be a String
}3. Concurrency
Python: Threading (limited by GIL)
ThreadingHTTPServer(server_address, RequestHandler)Rust: True async (no GIL)
#[tokio::main]
async fn main() {
// Truly concurrent async operations
}Run both servers independently and communicate via HTTP:
βββββββββββββββ HTTP ββββββββββββββββ
β Rust β βββββββββββββββΊ β Python β
β Server β β Service β
β (Port 3000)β β (Port 8000) β
βββββββββββββββ ββββββββββββββββ
Use Cases:
- Rust handles high-throughput API requests
- Python handles ML inference, data processing
- Both communicate via REST API
Embed Python in Rust or vice versa using PyO3:
use pyo3::prelude::*;
fn call_python() -> PyResult<()> {
Python::with_gil(|py| {
let sys = py.import("sys")?;
let version: String = sys.getattr("version")?.extract()?;
println!("Python version: {}", version);
Ok(())
})
}Use Cases:
- Call Python ML models from Rust
- Use Python libraries in Rust applications
- Gradual migration from Python to Rust
Run Python scripts from Rust:
use std::process::Command;
let output = Command::new("python3")
.arg("script.py")
.output()?;- π Rapid prototyping is priority
- π Heavy data science/ML workloads
- π§ Frequent changes and iterations
- π₯ Team is primarily Python developers
- π Rich ecosystem is needed (pandas, numpy, etc.)
- β‘ Performance is critical
- π Memory safety is required
- π High concurrency is needed
- π¦ Low resource usage is important
- π― Long-term stability is priority
- π¨ Prototype in Python, optimize in Rust
- ποΈ Microservices architecture
- π Different services have different needs
- π Python for data, Rust for API
- Start Here: Run both servers and compare their behavior
- Experiment: Modify endpoints in both implementations
- Benchmark: Test performance under load
- Translate: Try converting Python code to Rust
- Integrate: Experiment with PyO3 or HTTP communication
- Optimize: Identify bottlenecks and optimize accordingly
This is an educational project. Feel free to:
- Add new endpoints to both servers
- Implement additional features (authentication, database, etc.)
- Add more comprehensive benchmarks
- Document your findings and learnings
GPL-3.0 license
This project is for educational purposes. Use freely for learning and experimentation.
- Run the comparison:
python main.py compare - Read the translation guide:
docs/python_to_rust_translation.md - Experiment: Modify both servers and observe differences
- Benchmark: Test performance with real workloads
- Build: Create your own hybrid Python-Rust application
Happy Learning! π
Explore the differences, understand the trade-offs, and choose the right tool for your needs.