A simple Python library to build HTTP headers.
Project description
Http-Html A lightweight and complete Python library for building simple web servers.
Features Integrated Web Structure: Includes classes for Request, Response, Router, and Server.
Request Routing: Automatically directs each URL to a specific function using the Router class.
Easy Header Building: Build complex HTTP headers in a single line.
Automatic Request Parsing: Parses incoming requests (GET and POST) automatically.
Standard Status Codes: Supports standard HTTP status codes and their messages.
Installation You can install the library easily using pip once it's published on PyPI:
Bash
pip install http-html How to Use http-html This library is designed to make it simple to build a web server from scratch. All you need is a single Python file to run your server.
Step 1: Create Your Server First, create a Server object. This class handles the low-level socket connections and listens for incoming requests.
Python
main_server.py
from httpkit.server import Server
HOST = '0.0.0.0' PORT = 8080
Create your server instance
app = Server(HOST, PORT)
Start the server
if name == "main": app.run() Step 2: Define Your Routes Use the @app.route decorator to connect specific URLs to a function. The decorator takes two arguments: the path (e.g., "/", "/about") and the HTTP method (e.g., "GET", "POST").
Python
from httpkit.response import Response
@app.route("/", "GET") def index_page(client_connection, request): html_content = "
Welcome to Http-Html!
" # The handler function takes two arguments: # 1. client_connection: for sending the response # 2. request: the parsed request object# Use Response to build and send the final data
response = Response(200, "text/html", html_content)
client_connection.sendall(response.build())
@app.route("/contact", "GET") def contact_page(client_connection, request): html_content = "Contact us at my_email@example.com" response = Response(200, "text/html", html_content) client_connection.sendall(response.build()) Step 3: Handle Requests The request object provides easy access to all the information from the browser. You can access headers, the request method, and the path.
Python
@app.route("/user-info", "GET") def user_info(client_connection, request): # Access a header (e.g., User-Agent) user_agent = request.headers.get("User-Agent", "Not specified")
# Access the request path
path = request.path
# Access the request method (GET, POST)
method = request.method
# You can also get POST data from request.body
# ...
Step 4: Build a Response Use the Response class to build the final HTTP response. It handles everything for you and is a much better way than manually writing the headers.
Python
from httpkit.response import Response
This creates a complete HTTP response with headers and body
my_response = Response( 200, # Status Code (e.g., 200) "text/html", # Content Type "
Success!
" # Content )Use the .build() method to get the final bytes to send
final_bytes = my_response.build() License This project is licensed under the MIT License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file http_html-0.0.4.tar.gz.
File metadata
- Download URL: http_html-0.0.4.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8377a1a8768264317716460f56a9de2d8fdf9aeecd67ae0532c028492f91eff
|
|
| MD5 |
8ccc6a595df29a8a1cf346bc7e18f68e
|
|
| BLAKE2b-256 |
89a73d669f9eaa789ae169f95a99c65076a9ab2f9b067b5e5c97cfc255c62b6a
|
File details
Details for the file http_html-0.0.4-py3-none-any.whl.
File metadata
- Download URL: http_html-0.0.4-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71d60658ab925a052904436c8da0f93bfe60ec5867b50d9d6faa171645d73483
|
|
| MD5 |
554c952f8a4d8a68aab89cdca1393359
|
|
| BLAKE2b-256 |
51fcfd3ff535fd500a24965d0abc70077f639436a9da42935cf26022ee82dab5
|