This project is a personal learning project aimed at understanding the basics of HTTP servers. It is a simple implementation of an HTTP server in Python and is not production-ready or secure. Use it only for educational purposes.
- Basic HTTP server functionality.
- Support for adding custom handlers for different routes.
- Example handlers for serving files and JSON responses.
-
Clone the Repository
Clone this repository to your local machine. -
Install Dependencies
Ensure you have uv with Python installed. Install any required dependencies using:pip install -r requirements.txt
-
Run the Server
Start the server by running themain.pyfile:python main.py
-
Access the Server
By default, the server will run onhttp://localhost:8080. You can access the available routes in your browser or using tools likecurl.
To add a custom handler:
-
Create a New Handler Class
Create a new class that inherits from theHandlerbase class. Implement thehandle_connectionmethod to define how the server should respond to requests.Example:
from src.handler import Handler class CustomHandler(Handler): def handle_connection(self, verb, path, header_dict, body): status = '200 OK' response_body = 'Hello, this is a custom handler!' return f'HTTP/1.0 {status}\r\n\r\n{response_body}'.encode(), status
-
Register the Handler
Inmain.py, add the handler to the server for a specific route:from custom_handler import CustomHandler custom_handler = CustomHandler() server.add_handler('GET', '/custom', custom_handler)
-
Restart the Server
Restart the server to apply the changes.
This project is for educational purposes only. It is not secure or optimized for production use. Use it at your own risk.