A minimal HTTP routing framework based on Python’s built-in `http.server` for IPC tooling.
Project description
Miniroute
Miniroute is a minimal HTTP framework that only uses code from on Python’s built-in http.server, designed for lightweight local servers and IPC tooling.
It only adds 80 lines of code while everything it rests on is 100% native from python.
This tool is meant for really basic and local http communication, preferably inter-process.
For most of these kind of jobs, the standard http.server module is enough, but is a pain to integrate, in terms of boilerplate code.
Warning
As the officiel http module documentation state : This is NOT a server to be used in PRODUCTION.
This module doesn't address any security flaws that http may bring up.
It's encouraged to be familiar with http.server documentation before using miniroute
Installation
pip install miniroute
Usage
The Miniroute class inherits the http.server.HTTPServer class, and integrates a router by
having its own RequestHandlerClass which always replaces the one you would pass.
It also has a run method that is only a renamed serve_forever method.
Apart from that, it can be used exactly as you would use a http.server.HTTPServer object.
from miniroute import Miniroute
import json
app = Miniroute(host="localhost", port=5000)
@app.router.get("/hello")
def hello(handler):
body = json.dumps({"message": "Hello, world!"}).encode()
headers = [("Content-Type", "application/json")]
return 200, headers, body
@app.router.post("/echo")
def echo(handler):
length = int(handler.headers.get("Content-Length", 0))
data = handler.rfile.read(length)
return 200, [("Content-Type", "application/json")], data
if __name__ == "__main__":
app.run()
Route handlers
Each route function:
- Receives the current
BaseHTTPRequestHandleras argument - Must return a tuple:
status_code: int, headers: dict[str, str], body: bytes
Example:
return 200, [("Content-Type", "text/plain")], b"OK"
License
PSF License You are free to use, modify, and distribute this software under the terms of the Python Software Foundation License.
This project is not affiliated with or endorsed by the Python Software Foundation.
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 miniroute-0.2.4.tar.gz.
File metadata
- Download URL: miniroute-0.2.4.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.2 Linux/6.1.0-33-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cda4f46abbab8056e6288b9bd9b649a44929ac7f5762fa0e9e4e936c42c3a96
|
|
| MD5 |
d1cd09fb038ff596a867716574bd163f
|
|
| BLAKE2b-256 |
305dc81bf39fa4766662882f51ceeb3d731a8a860fe90111632569ebddd73c2a
|
File details
Details for the file miniroute-0.2.4-py3-none-any.whl.
File metadata
- Download URL: miniroute-0.2.4-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.2 Linux/6.1.0-33-amd64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f679e3939775a1c938cab564ef2c520a6a34aa1404cadc42e44340b8589f8bd8
|
|
| MD5 |
fa3ee889cada3cfaee0308060d1af957
|
|
| BLAKE2b-256 |
2ae2e0cce1785b87c92af849de09f6c3cb4b874797d66836dde2c0bb83652286
|