A lightweight HTTP proxy server implementation in Python that includes caching capabilities. This proxy server acts as an intermediary between clients and web servers, with the ability to cache responses for improved performance.
- HTTP Request Forwarding: Forwards client requests to target web servers
- Response Caching: Caches successful responses for faster subsequent access
- Size-Limited Caching: Implements a 16MB size limit for cached files
- Error Handling: Manages various HTTP response scenarios
- Buffer Management: Efficient data transfer with 4KB buffer size
- Configurable Port: Dynamic port assignment based on input
- Python 3.x
- Basic understanding of HTTP protocols
- Socket programming knowledge
- Clone the repository:
git clone [repository-url]
cd [repository-name]- Ensure you have Python 3.x installed:
python --version- No additional dependencies required (uses standard Python libraries)
- Start the proxy server:
python3 proxy.py <port>Example:
python3 proxy.py 8080- The actual port will be calculated as:
actual_port = input_port + (4196840 % 100)- The server will start listening on localhost at the calculated port
Key constants that can be modified in the code:
MAX_FILE_SIZE = 16 * 1024 * 1024 # Maximum cache file size (16MB)
CACHE_DIR = "./cache" # Cache directory location
BUFFER_SIZE = 4096 # Buffer size for data transfer.
├── proxy.py # Main proxy server implementation
├── cache/ # Directory for cached responses
└── README.md # Project documentation
- Creates socket server
- Listens for incoming connections
- Handles client connections
def handle_client(client_socket):
# Handles incoming client requests
# Checks cache and serves/forwards accordinglydef parsed_url(request):
# Parses HTTP requests
# Extracts method, host, port, and pathdef forward_request(client_socket, host, port, path, method, http_version):
# Forwards requests to target servers
# Manages server connections- Client sends request to proxy
- Proxy checks cache for requested content
- If cached:
- Serves content from cache
- If not cached:
- Forwards request to web server
- Receives response
- Caches if response is successful (200)
- Sends response to client
- Returns 500 Internal Error for problematic responses
- Handles connection errors gracefully
- Validates request formats
- Only handles basic HTTP requests
- No HTTPS support
- Maximum cache size of 16MB per file
- Basic error handling
- No concurrent connection handling
- Cached files stored in
./cachedirectory - Automatic cache directory creation
- Size-limited caching (16MB max)
- Caches only successful (200) responses
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
- For educational purposes
- Basic implementation - not recommended for production use
- Can be extended for more complex scenarios
- Add HTTPS support
- Implement concurrent connection handling
- Add cache expiration
- Improve error handling
- Add request/response logging
- Implement cache size management
- Add support for more HTTP methods
Krushikesh Thotange