Skip to main content

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 BaseHTTPRequestHandler as 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

miniroute-0.2.1.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

miniroute-0.2.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file miniroute-0.2.1.tar.gz.

File metadata

  • Download URL: miniroute-0.2.1.tar.gz
  • Upload date:
  • Size: 3.9 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

Hashes for miniroute-0.2.1.tar.gz
Algorithm Hash digest
SHA256 c77e32c66bb8355033b7fe0e9c94f368e1f025ab422636eee7545cf577a997d2
MD5 4c9899583aeb443dc8160cf36807610f
BLAKE2b-256 bb031df6a3cdb52d00b2d3aa8b4848429e7a63249e354862d3d3ca78d31ce33a

See more details on using hashes here.

File details

Details for the file miniroute-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: miniroute-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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

Hashes for miniroute-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d12e6c97e26095cd52a9a929cb3192114fd5ae15d50f93f23fee18db24cdbd5c
MD5 79472efe71c97a534d0c1fd175a08215
BLAKE2b-256 81e24c3d16fc0c51839e23a01d39c016476ee5fc9356fb5c16423b0c040f8553

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page