Skip to main content

Async-first server toolkit with plugins, native WebSocket support, auth/rate limiting, and TLS. GitHub: https://github.com/ythbambaranda-sys/yashserver

Project description

yashserver

yashserver is an independent Python server library focused on simple APIs:

  • YServer for line-based TCP command servers
  • YWebSocketServer for WebSocket routes
  • YHttpServer for lightweight HTML/API serving
  • YSyncServer, YSyncWebSocketServer, YSyncHttpServer for non-async usage
  • Built-in TLS (ssl_context) support
  • Optional token auth and sliding-window rate limiting
  • Plugin hooks for lifecycle, traffic, and error handling

Core networking is standard library based. Database connector dependencies are included in the default install.

Install

pip install -e .

From PyPI:

pip install yashserver

Task 1 Demo (Browser + HTML)

Run:

python examples/task1_browser_server.py

Then open:

http://127.0.0.1:8080

You will see a small HTML page served by YHttpServer. Live chat is enabled by YWebSocketServer with no external dependencies. The page code is in test.html, served on / and /test.html.

Demo highlights:

  • Chunked binary file upload (no base64 transfer path), including videos
  • ACK/retry chunk handling in the browser client
  • Server-side file storage + /download/<id> links
  • Optional auth token via env var YSERVER_TOKEN
  • Optional MongoDB text-message persistence

Mongo text persistence env vars for examples/task1_browser_server.py:

  • YSERVER_MONGO_TEXT_ENABLED (1 or 0)
  • YSERVER_MONGO_URI (example: mongodb://localhost:27018)
  • YSERVER_MONGO_DB (default: yserver_chat)
  • YSERVER_MONGO_COLLECTION (default: messages)
  • YSERVER_MONGO_GRIDFS_BUCKET (default: fs, creates <bucket>.files + <bucket>.chunks)
  • YSERVER_MONGO_GRIDFS_CHUNK_SIZE (default: 524288 bytes)
  • YSERVER_MONGO_WRITE_CONCERN (default: 1 for fast local visibility in Compass)
  • YSERVER_MONGO_JOURNAL (1 or 0, default: 0)
  • or set YSERVER_MONGO_HOST + YSERVER_MONGO_PORT instead of a full URI

Minimal Sync Example

import yserver

app = yserver.YSyncServer(port=9000)

@app.route("ping")
def ping(client, payload, server):
    return {"reply": "pong"}

app.run()

You can use a single import and access all public helpers from yserver.*, for example: yserver.ConnectionStatsPlugin, yserver.LoggingPlugin, yserver.ServerTools, yserver.YSyncHttpServer, yserver.YSyncWebSocketServer, and yserver.run_many.

Database Support

Single import API:

import yserver

db = yserver.connect_database("sqlite", database=":memory:")
db.execute("CREATE TABLE users (id INTEGER, name TEXT)")
db.execute("INSERT INTO users (id, name) VALUES (?, ?)", (1, "Ada"))
print(db.fetch_all("SELECT * FROM users"))
db.close()

Also available through tools:

db = yserver.ServerTools.connect_database("sqlite", database="app.db")
print(yserver.ServerTools.supported_databases())

Supported backends:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • SQLite
  • MariaDB
  • MongoDB
  • Redis
  • Cassandra
  • DynamoDB
  • Firebase Realtime Database
  • Couchbase
  • Snowflake
  • Google BigQuery
  • Amazon Redshift
  • ClickHouse
  • Elasticsearch
  • Neo4j
  • InfluxDB
  • DuckDB

Quick DB demo script:

python examples/task2_database_support.py

MongoDB Compass quick test (default local port 27018):

python examples/task3_mongodb_compass_test.py

Compass connection string:

mongodb://localhost:27018

Notes:

  • SQL backends use SQLAlchemy (url= or DSN-style config).
  • SQLite is supported via stdlib sqlite3.
  • Non-SQL backends use their ecosystem drivers (installed by default with yashserver).

TLS / HTTPS

import yserver

ssl_ctx = yserver.ServerTools.create_server_ssl_context(
    certfile="cert.pem",
    keyfile="key.pem",
)

http = yserver.YSyncHttpServer(port=8443, ssl_context=ssl_ctx)
ws = yserver.YSyncWebSocketServer(port=9443, ssl_context=ssl_ctx)

For the demo script (examples/task1_browser_server.py), you can enable TLS by env vars:

set YSERVER_TLS_CERT=cert.pem
set YSERVER_TLS_KEY=key.pem
python examples/task1_browser_server.py

If tls/cert.pem and tls/key.pem exist in the project root, the demo auto-enables HTTPS/WSS.

Auth + Rate Limit

import yserver

ws = yserver.YSyncWebSocketServer(
    auth_token="my-secret-token",
    rate_limit_per_window=300,
    rate_limit_window_seconds=60.0,
)

http = yserver.YSyncHttpServer(
    auth_token="my-secret-token",
    rate_limit_per_window=800,
    rate_limit_window_seconds=60.0,
)

Token can be provided as:

  • Query param: ?token=...
  • Header: x-yserver-token: ...
  • Header: Authorization: Bearer ...

Test Suite

python -m unittest discover -s tests -p "test_*.py" -v

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

yashserver-0.1.3.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

yashserver-0.1.3-py3-none-any.whl (24.8 kB view details)

Uploaded Python 3

File details

Details for the file yashserver-0.1.3.tar.gz.

File metadata

  • Download URL: yashserver-0.1.3.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for yashserver-0.1.3.tar.gz
Algorithm Hash digest
SHA256 08ce70a73995c08b0d039a868383b3943180f9600173a6cc7f346ea2ec3c16a4
MD5 bbf6a187558470f5ad6ce292b69fd765
BLAKE2b-256 811a9088fab780f1b50a21f6aeaaa9bb29cb4889161d6c3bd17476bf8cff53a2

See more details on using hashes here.

File details

Details for the file yashserver-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: yashserver-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 24.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for yashserver-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0d76d2685842a5eb57dd533b427a67fc094eb9f5dfe4960e7bbae93e503a6458
MD5 64efe46803ca8e163e9e5ca60d71d6f4
BLAKE2b-256 a5a2d6635ff6e8eb2914165393ef5ce1f145d95c2b0e8b7306ce1261fa3c40cf

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