A fully-featured HTTP server implementation using low-level socket programming in Python.
cn_assignment/
├── server.py # Main server implementation
├── resources/ # Static files directory
│ └── uploads/ # Directory for POST uploads
└── README.md
- Python 3.7 or higher
- No external dependencies required (uses standard library only)
python server.pypython server.py [port] [host] [thread_pool_size]Examples:
python server.py 8000 # Run on port 8000
python server.py 8000 0.0.0.0 # Run on all interfaces
python server.py 8000 0.0.0.0 20 # With 20 threads✅ Core HTTP Server
- TCP socket programming with proper lifecycle management
- HTTP/1.0 and HTTP/1.1 protocol support
- Request parsing and response generation
- Support for GET and POST methods
✅ Multi-threading
- Fixed-size thread pool (configurable, default: 10)
- Connection queue for overflow handling
- Thread-safe operations with mutex locks
- Worker thread lifecycle management
✅ File Serving
- HTML files rendered in browser
- Binary file downloads (images, text files)
- Proper Content-Type and Content-Disposition headers
- Support for .html, .txt, .png, .jpg, .jpeg files
✅ JSON Upload
- POST request handling with JSON validation
- Content-Type validation
- Timestamped unique filenames
- Automatic uploads directory creation
✅ Security
- Path traversal protection
- Host header validation
- Input sanitization
- Request size limits
✅ Connection Management
- Keep-Alive support (persistent connections)
- 30-second timeout for idle connections
- Maximum 100 requests per connection
- Proper connection closure
✅ Error Handling
- Comprehensive HTTP status codes (200, 201, 400, 403, 404, 405, 415, 500)
- Graceful error recovery
- Detailed error logging
- Navigate to
http://localhost:8080/to view index.html - Try
http://localhost:8080/about.htmlfor other pages - Download files by clicking links on the pages
GET HTML file:
curl http://localhost:8080/Download binary file:
curl -O http://localhost:8080/sample.txtPOST JSON data:
curl -X POST http://localhost:8080/upload -H "Content-Type: application/json" -H "Host: localhost:8080" -d @test_upload.jsonTest security (should return 403):
curl http://localhost:8080/../etc/passwdTest error handling (should return 404):
curl http://localhost:8080/nonexistent.htmlRun the comprehensive test suite:
python test_server.pyThis will test:
- GET requests (HTML and binary files)
- POST requests (JSON uploads)
- Security features (path traversal, host validation)
- Error handling (404, 403, 405, 415)
- Keep-Alive connections
cn_assignment/
├── server.py # Main server implementation
├── test_server.py # Automated test suite
├── test_upload.json # Sample JSON for POST testing
├── README.md # Project overview and usage
├── TECHNICAL_DOCS.md # Detailed technical documentation
└── resources/ # Static files directory
├── index.html # Home page
├── about.html # About page
├── contact.html # Contact page
├── sample.txt # Test text file
├── document.txt # Test document
└── uploads/ # Directory for POST uploads
- Pre-spawned worker threads
- Connection queue for overflow handling
- Mutex-based synchronization
- Configurable pool size
- Files read in binary mode
- Content sent as application/octet-stream
- Content-Disposition header triggers downloads
- Supports large files (tested >1MB)
- Path Traversal: Validates all paths stay within resources directory
- Host Validation: Ensures requests are intended for this server
- Input Validation: Checks request format, JSON validity, Content-Type
- Concurrent connections: Limited by thread pool size (default: 10)
- Requests per connection: Up to 100 (Keep-Alive)
- Connection timeout: 30 seconds
- Request size limit: 8192 bytes
For detailed technical documentation, see TECHNICAL_DOCS.md
| Code | Meaning | When Used |
|---|---|---|
| 200 | OK | Successful GET request |
| 201 | Created | Successful POST request |
| 400 | Bad Request | Malformed request, missing Host, invalid JSON |
| 403 | Forbidden | Path traversal attempt, Host mismatch |
| 404 | Not Found | Requested file doesn't exist |
| 405 | Method Not Allowed | Unsupported HTTP method (PUT, DELETE, etc.) |
| 415 | Unsupported Media Type | Wrong Content-Type or unsupported file type |
| 500 | Internal Server Error | Unexpected server error |
- No HTTPS/SSL support
- No response compression
- No caching mechanisms
- Limited to GET and POST methods
- Single-process (cannot scale across CPU cores)
- Python GIL limitations for CPU-bound tasks