Skip to main content

Framework ligero HTTP + WebSocket.

Project description

wsbuilder

wsbuilder es un paquete Python para construir servidores HTTP + WebSocket sin dependencias externas.

Lightweight Python HTTP + WebSocket framework for building real-time APIs and custom servers.

Keywords: python, http-server, websocket, framework, real-time, api, socket.

Incluye

  • wsbuilder.framework: router, request/response, servidor HTTP, handshake WS y utilidades de frames.
  • wsbuilder.orm: ORM flexible para SQLite3 (modelos, QuerySet, filtros, transacciones anidadas).
  • wsbuilder.ws_demo: demo completo HTTP + WebSocket + REST + SQLite.

Instalacion local

python -m pip install -e .

Uso rapido del framework

from wsbuilder import App, Response, parse_close_payload

app = App()

@app.view("/")
def home(_request):
    return Response.html("<h1>wsbuilder</h1>")

@app.api("/api/health")
def health(_request):
    return {"ok": True}

@app.ws("/ws/")
def ws_handler(ws, _request):
    while True:
        fin, opcode, payload, _masked, _mask = ws.recv_frame()
        if opcode == 0x8:
            code, reason = parse_close_payload(payload)
            ws.close(code or 1000, reason or "")
            break
        if opcode == 0x9:
            ws.send_pong(payload)
            continue
        if opcode == 0x1:
            ws.send_text(payload.decode("utf-8", errors="ignore"))
        elif opcode == 0x2:
            ws.send_binary(payload)

app.run("0.0.0.0", 8765)

Para CORS sin variables de entorno:

app = App(cors_allow_origin="*")

ORM SQLite3 rapido

from wsbuilder import Database, IntegerField, Model, TextField

class User(Model):
    id = IntegerField(primary_key=True, auto_increment=True)
    username = TextField(unique=True, index=True, null=False)
    email = TextField(null=False)

db = Database("app.db")
User.create_table(db)

u = User(username="alice", email="alice@example.com")
u.save(db)

admins = User.objects(db).filter(username__contains="ali").order_by("-id").all()
print([x.to_dict() for x in admins])

Metrics (JSON stream)

from wsbuilder import App

app = App()
app.enable_metrics()  # crea /api/metrics y /api/metrics/stream

Probar snapshot:

curl http://127.0.0.1:8765/api/metrics

Probar stream NDJSON:

curl -N "http://127.0.0.1:8765/api/metrics/stream?interval=1&limit=5"

Modo continuo (no termina solo):

curl -N "http://127.0.0.1:8765/api/metrics/stream?interval=1&follow=1"

HTTP streaming (chunked)

import time
from wsbuilder import App, Response

app = App()

@app.view("/stream")
def stream(_request):
    def chunks():
        for i in range(5):
            yield f"chunk {i}\n"
            time.sleep(1)
    return Response.stream(chunks(), content_type="text/plain; charset=utf-8")

Ejecutar demo incluido

python -m wsbuilder --host 0.0.0.0 --port 8765
# o
wsbuilder --host 0.0.0.0 --port 8765

Configuracion CORS

  • Usa App(cors_allow_origin="https://tu-dominio.com").
  • Usa App(cors_allow_origin="*") para permitir cualquier origen.

CI/CD (GitHub Actions)

  • package-build.yml: construye sdist + wheel, valida metadata y prueba instalacion/import.
  • release-from-main.yml en push a main: calcula semver automaticamente, crea rama release/v<version> desde main, actualiza version del paquete, crea tag v<version> y publica GitHub Release.
  • publish-packages.yml en push de tag v*: construye y publica a PyPI/TestPyPI (instalable con pip).
  • publish-packages.yml en tags v*: adjunta los paquetes al GitHub Release.
  • main-only-from-develop.yml (workflow main-pr-source-check): valida metadata minima del PR hacia main.

Reglas de versionado automatico

  • major: si algun commit trae BREAKING CHANGE, type(scope)!: o ramas/commits marcados como breaking/major.
  • minor: si hay suficiente peso de feat/feature (proporcional frente a cambios patch) y no hay major.
  • patch: cualquier otro caso.

Recomendado usar Conventional Commits para que el calculo de version sea preciso.

Secrets requeridos

  • RELEASE_BOT_TOKEN (recomendado): PAT/fine-grained token para crear rama release/*, crear tag y publicar GitHub Release.
  • RULESET_ADMIN_TOKEN (fallback): usado automaticamente si no existe RELEASE_BOT_TOKEN.
  • PYPI_API_TOKEN: token de publicacion para PyPI.
  • TEST_PYPI_API_TOKEN (opcional): token de publicacion para TestPyPI.

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

wsbuilder-0.3.0.tar.gz (39.2 kB view details)

Uploaded Source

Built Distribution

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

wsbuilder-0.3.0-py3-none-any.whl (40.1 kB view details)

Uploaded Python 3

File details

Details for the file wsbuilder-0.3.0.tar.gz.

File metadata

  • Download URL: wsbuilder-0.3.0.tar.gz
  • Upload date:
  • Size: 39.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wsbuilder-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1f8a9b264ad4092eab933ff728598d7b52afba28d517088e32b67894a6ae6ab4
MD5 865c99ea2e1e42436085f3f7ad81cf3f
BLAKE2b-256 0d657b56cc9ed62e0f8e6161fdadadc8bbbbcd7b7753fbd0434124ecee06303f

See more details on using hashes here.

File details

Details for the file wsbuilder-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: wsbuilder-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 40.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for wsbuilder-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed012054fc9914b0bb43d397efad14f58f091956b38faa3969da0ca61a228a8e
MD5 86c387f4428d26a467e552c2abba2626
BLAKE2b-256 025997153e081a4b813060fc5cba6807604c7d6b0c6723ca8aca4600adbb0017

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