Skip to content

A multi-threaded HTTP server built using socket programming that handles concurrent clients, serves static and binary files, processes JSON requests, and implements key HTTP/1.1 features with security and logging.

Notifications You must be signed in to change notification settings

asmitbanik/Multi-Threaded-HTTP-Server-using-Socket-Programming

Repository files navigation

Multi-threaded HTTP Server

A fully-featured HTTP server implementation using low-level socket programming in Python.

Project Structure

cn_assignment/
├── server.py              # Main server implementation
├── resources/             # Static files directory
│   └── uploads/          # Directory for POST uploads
└── README.md

Requirements

  • Python 3.7 or higher
  • No external dependencies required (uses standard library only)

Usage

Basic Usage

python server.py

Custom Configuration

python 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

Features Summary

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

Testing

Using a Web Browser

  • Navigate to http://localhost:8080/ to view index.html
  • Try http://localhost:8080/about.html for other pages
  • Download files by clicking links on the pages

Using curl

GET HTML file:

curl http://localhost:8080/

Download binary file:

curl -O http://localhost:8080/sample.txt

POST JSON data:

curl -X POST http://localhost:8080/upload -H "Content-Type: application/json" -H "Host: localhost:8080" -d @test_upload.json

Test security (should return 403):

curl http://localhost:8080/../etc/passwd

Test error handling (should return 404):

curl http://localhost:8080/nonexistent.html

Automated Testing

Run the comprehensive test suite:

python test_server.py

This 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

Project Structure

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

Technical Details

Thread Pool Architecture

  • Pre-spawned worker threads
  • Connection queue for overflow handling
  • Mutex-based synchronization
  • Configurable pool size

Binary File Transfer

  • Files read in binary mode
  • Content sent as application/octet-stream
  • Content-Disposition header triggers downloads
  • Supports large files (tested >1MB)

Security Measures

  • 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

Performance

  • 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

HTTP Status Codes

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

Known Limitations

  • 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

About

A multi-threaded HTTP server built using socket programming that handles concurrent clients, serves static and binary files, processes JSON requests, and implements key HTTP/1.1 features with security and logging.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published