{"id":18359148,"url":"https://github.com/theinit01/httpserver","last_synced_at":"2025-04-10T03:17:40.807Z","repository":{"id":211478357,"uuid":"729272738","full_name":"theinit01/HTTPServer","owner":"theinit01","description":"A lightweight Python implementation of a basic HTTP server. Designed for educational purposes, it allows you to handle simple HTTP GET requests, serve static files, and understand the fundamentals of web server functionality.","archived":false,"fork":false,"pushed_at":"2023-12-12T05:10:20.000Z","size":370,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T03:17:40.064Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/theinit01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-08T19:23:01.000Z","updated_at":"2023-12-12T05:35:19.000Z","dependencies_parsed_at":"2023-12-12T05:39:54.272Z","dependency_job_id":null,"html_url":"https://github.com/theinit01/HTTPServer","commit_stats":null,"previous_names":["theinit01/httpserver"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theinit01%2FHTTPServer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theinit01%2FHTTPServer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theinit01%2FHTTPServer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theinit01%2FHTTPServer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theinit01","download_url":"https://codeload.github.com/theinit01/HTTPServer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248148247,"owners_count":21055548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T22:21:05.457Z","updated_at":"2025-04-10T03:17:40.787Z","avatar_url":"https://github.com/theinit01.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HTTPServer\n\n## Overview\n\nHTTPServer is a simple implementation of an HTTP server in Python, designed for educational purposes. This server is capable of handling basic HTTP GET requests, serving static files, and responding to different HTTP status codes. The server is implemented using the socket library in Python.\n\n## How it Works\n\n### 1.  TCPServer Class\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"src/tcp-socket.png\" alt=\"Creating a tcp server as base\"\u003e\n\u003c/p\u003e\n\n\nThe `TCPServer` class serves as a generic TCP server and provides a foundation for the HTTP server. It initializes a socket, binds it to a specified host and port, and listens for incoming connections. The `start` method of `TCPServer` listens for incoming connections and, upon connection, handles the request using the `handle_request` method.\n\n### 2.  HTTPServer Class\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"src/http-speak.png\" alt=\"Teaching HTTP protocol to our server\"\u003e\n\u003c/p\u003e\n\nThe `HTTPServer` class is a subclass of `TCPServer` and is specifically tailored for handling HTTP requests. It overrides the `handle_request` method to determine the appropriate action based on the request method (GET, POST, etc.). The server supports basic HTTP status codes (200, 404, 403, 501) and has corresponding handlers for each.\n\n### 3 . HTTPRequest Class\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"src/req.jpg\" alt=\"Typical HTTP request\"\u003e\n\u003c/p\u003e\n\nThe `HTTPRequest` class is responsible for parsing incoming HTTP requests. It extracts information such as the request method, URI, and HTTP version from the raw request data.\n\n### 4.  Handling GET Requests\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"src/reqline.png\" alt=\"request\"\u003e\n\u003c/p\u003e\n\nThe server includes a handler for GET requests (`handle_GET`). It processes the requested file, checks its existence, determines the MIME type, and sends an appropriate response.\n\n###  5. Handling Status Codes\n\nThe server includes handlers for specific status codes, such as 404 (Not Found), 403 (Access Forbidden), and 501 (Not Implemented). These handlers generate corresponding response pages.\n\n## Code Explanation\n\nThe code is well-organized and follows a modular structure:\n\n- **TCPServer:** Generic TCP server for handling connections.\n- **HTTPServer:** Specific implementation for handling HTTP requests, extending the TCPServer class.\n- **HTTPRequest:** Parses incoming HTTP requests and extracts relevant information.\n\nThe server responses include HTTP headers such as \"Server\" and \"Content-Type\" along with the appropriate status code and response body.\n\n## How to Use\n\nTo use the HTTP server, simply create an instance of the `HTTPServer` class and call the `start` method. By default, the server listens on `127.0.0.1:8888`.\n\n```python\nif __name__ == '__main__':\n    server = HTTPServer()\n    server.start()\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheinit01%2Fhttpserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheinit01%2Fhttpserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheinit01%2Fhttpserver/lists"}