A familiar HTTP Service Framework for Python.
Project description
Responder
A familiar HTTP Service Framework for Python, powered by Starlette.
import responder
api = responder.API()
@api.route("/{greeting}")
async def greet_world(req, resp, *, greeting):
resp.text = f"{greeting}, world!"
if __name__ == "__main__":
api.run()
$ pip install responder
That's it. Supports Python 3.10+.
The Basics
resp.textsends back text.resp.htmlsends back HTML.resp.contentsends back bytes.resp.mediasends back JSON (or YAML, with content negotiation).resp.file("path.pdf")serves a file with automatic content-type detection.req.headersis case-insensitive.req.paramsgives you query parameters.- Both sync and async views work — the
asyncis optional.
Highlights
# Type-safe route parameters
@api.route("/users/{user_id:int}")
async def get_user(req, resp, *, user_id):
resp.media = {"id": user_id}
# HTTP method filtering
@api.route("/items", methods=["POST"])
async def create_item(req, resp):
data = await req.media()
resp.media = {"created": data}
# Class-based views
@api.route("/things/{id}")
class ThingResource:
def on_get(self, req, resp, *, id):
resp.media = {"id": id}
def on_post(self, req, resp, *, id):
resp.text = "created"
# Before-request hooks (auth, rate limiting, etc.)
@api.route(before_request=True)
def check_auth(req, resp):
if not req.headers.get("Authorization"):
resp.status_code = 401
resp.media = {"error": "unauthorized"}
# Custom error handling
@api.exception_handler(ValueError)
async def handle_error(req, resp, exc):
resp.status_code = 400
resp.media = {"error": str(exc)}
# Lifespan events
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app):
print("starting up")
yield
print("shutting down")
api = responder.API(lifespan=lifespan)
# GraphQL
import graphene
api.graphql("/graphql", schema=graphene.Schema(query=Query))
# WebSockets
@api.route("/ws", websocket=True)
async def websocket(ws):
await ws.accept()
while True:
name = await ws.receive_text()
await ws.send_text(f"Hello {name}!")
# Mount WSGI/ASGI apps
from flask import Flask
flask_app = Flask(__name__)
api.mount("/flask", flask_app)
# Background tasks
@api.route("/work")
def do_work(req, resp):
@api.background.task
def process():
import time; time.sleep(10)
process()
resp.media = {"status": "processing"}
Built-in OpenAPI docs, cookie-based sessions, gzip compression, static file serving, Jinja2 templates, and a production uvicorn server.
Route convertors: str, int, float, uuid, path.
Documentation
Project details
Release history Release notifications | RSS feed
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 responder-3.6.0.tar.gz.
File metadata
- Download URL: responder-3.6.0.tar.gz
- Upload date:
- Size: 60.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
acd37563d83303cf3794a773d9dcadc4e9ef62625186f041f5d170fc2c4ed1f1
|
|
| MD5 |
8c3f17087bbe96254962536ae3c088a0
|
|
| BLAKE2b-256 |
e897c8cc8c943ac8fbbb276aea2dde75e69c85ce0ce2dbcaa2fe1b9c59ac1f54
|
File details
Details for the file responder-3.6.0-py3-none-any.whl.
File metadata
- Download URL: responder-3.6.0-py3-none-any.whl
- Upload date:
- Size: 49.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d76431117500dfae581c093a678404c48d51a798d79cd4b05ac8d019060e0d04
|
|
| MD5 |
a5639314ac4f20ccc4926dcf25a38446
|
|
| BLAKE2b-256 |
827ba242db89d4a29f6e48f3d22125f17f5b79dbdae2dba6d1a501e4681d1a0d
|