Skip to main content

SPWeb v0.2.0

A secure, Flask-like Python WSGI micro-framework. Zero dependencies beyond the standard library.

Quick start

from spweb import SPWeb, Response

app = SPWeb(secret_key="your-long-random-secret")

@app.route("/")
def index(request):
    return Response.html("<h1>Hello from SPWeb!</h1>")

@app.route("/api/users/<int:user_id>")
def get_user(request, user_id):
    return Response.json({"id": user_id, "name": "Alice"})

@app.post("/api/login")
def login(request):
    data = request.json
    # ... validate credentials ...
    return Response.json({"token": "..."})

app.run()

URL parameters

Supports typed URL parameters — <int:id>, <float:n>, <str:name>, <uuid:uid>, <path:rest>.

@app.route("/files/<path:filepath>")
def serve_file(request, filepath):
    ...

Response helpers

Response("plain text")
Response.html("<h1>Hi</h1>")
Response.json({"key": "value"})
Response.redirect("/new-url")
Response.empty(204)

# Chaining
Response.json(data).set_cookie("session", token, max_age=3600)

Request helpers

request.json        # parsed JSON body
request.form        # parsed form body
request.text        # raw body string
request.cookies     # dict of cookies
request.query       # dict of query params
request.url_params  # dict of matched URL params
request.remote_addr # client IP (X-Forwarded-For aware)
request.is_json     # bool
request.get_header("Authorization")

Hooks & error handlers

@app.before_request
def auth_check(request):
    if request.path.startswith("/admin"):
        if not request.cookies.get("session"):
            return Response.redirect("/login")

@app.after_request
def add_cors(request, response):
    response.set_header("Access-Control-Allow-Origin", "https://myapp.com")
    return response

@app.error_handler(404)
def not_found(request):
    return Response.html("<h1>Page not found</h1>", status=404)

HTTP exceptions

Raise from any handler:

from spweb import HTTPForbidden, HTTPNotFound

@app.get("/secret")
def secret(request):
    if not is_admin(request):
        raise HTTPForbidden("Admins only")
    raise HTTPNotFound("No such resource")

Security features (automatic)

Every response gets these headers out of the box:

Header Value
Content-Security-Policy strict default-src 'self' policy
Strict-Transport-Security 1-year HSTS + subdomains
X-Content-Type-Options nosniff
X-Frame-Options DENY
Referrer-Policy strict-origin-when-cross-origin
Cross-Origin-Opener-Policy same-origin
Cross-Origin-Embedder-Policy require-corp
Permissions-Policy blocks camera, mic, geolocation, etc.
Cache-Control no-store

Plus:

  • CSRF protection — HMAC-signed, timestamped tokens; auto-rotated on expiry.
  • Rate limiting — per-IP sliding window (default 100 req/60s).
  • Host validation — optional allowlist.
  • Origin validation — optional allowlist.
  • Body size cap — 16 MB default.

Configuration

app = SPWeb(
    debug=False,
    secret_key="...",                   # HMAC key for CSRF tokens
    allowed_hosts=["example.com"],      # block host-header injection
    trusted_origins=["https://example.com"],
    csrf_exempt_paths=["/webhook"],     # skip CSRF for specific paths
    rate_limit=200,                     # requests per window
    rate_limit_window=60,               # seconds
    hsts_max_age=63_072_000,            # 2 years
    hsts_preload=True,
    csp_policy="default-src 'self'",    # override CSP
)

Metadata

Release files for spweb 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spweb 0.2.0
File Size Uploaded Provenance Actions
spweb-0.2.0.tar.gz 11.9 kB Without provenance Details for spweb-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for spweb 0.2.0
File Interpreter ABI Platform Actions
spweb-0.2.0-py3-none-any.whl Python 3 none any Details for spweb-0.2.0-py3-none-any.whl

Total release size: 26.2 kB

Release files / spweb-0.2.0.tar.gz

Download URL spweb-0.2.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c05698e555feb5275ac21d5561a87dd354736ad62eeb8c82fd51ac94d78f1bab
BLAKE2b-256 checksum
How to use checksums
41c9410e97280510090daf02444d529ea28c93e8340b9033ba3d5b561d5d4da3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.6

Release files / spweb-0.2.0-py3-none-any.whl

Download URL spweb-0.2.0-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05a853b0d4e395da82c10fae21a93d8e91755e1042a5f9458ea28716b281c840
BLAKE2b-256 checksum
How to use checksums
89bb10dca42c621c361d0b0ac406a9c15024b44f0ebd32a66650f3893283f0cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.6

Security for spweb 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page