Skip to main content

⚡️Thunderlight is the fastest Python HTTP Server

Thunderlight is the fastest Python HTTP Server. It's dedicated for micro service and JSON API. In the future, a C version of this API is also supported. You can write Python or C in your server project to speed things up.

Installation

Install Thunderlight with pip.

pip install thunderlight

Documentation

get

Register a get request handler to the default app.

@get('/posts/:id')
async def posts(ctx: Ctx) -> None:
    ctx.res.json((await Post.id(ctx.req.args.id)).tojson())

post

Register a post request handler to the default app.

@post('/posts')
async def posts(ctx: Ctx) -> None:
    ctx.res.json(Post(**(await ctx.req.json())).save().tojson())

patch

Register a patch request handler to the default app.

@patch('/posts/:id')
async def posts(ctx: Ctx) -> None:
    ctx.res.json((await Post.id(ctx.req.args.id)).set(await ctx.req.json()).save().tojson())

delete

Register a delete request handler to the default app.

@delete('/posts/:id')
async def posts(ctx: Ctx) -> None:
    ctx.res.empty((await Post.id(ctx.req.args.id)).delete())

use

Append this middleware to the default app's middleware stack.

@use
async def measure_time(ctx: Ctx, next: Next) -> None:
    start = time()
    await next(ctx)
    time_elapsed = time() - start
    print(f'request {ctx.req.path} takes {time_elapsed} second.')

apply

Apply a middleware to a request handler. This is aka middleware for a single request handler.

async def validate_user(ctx: Ctx, next: Next) -> None:
    if ctx.req.headers['Authorization'] is not None:
        await next(ctx)
    else:
        ctx.res.code = 401
        ctx.res.json({"error": {"type": "Unauthorized"}})


@get('/users')
@apply(validate_user)
async def users(ctx: Ctx) -> None:
    ctx.res.json(await User.find())

App

Create a new server application.

app = App()

@app.get('/articles')
async def articles(ctx: Ctx) -> None:
    ctx.res.json(await Article.find())

Ctx

The context represents a request session.

ctx.req

The request object. This object is readonly.

ctx.res

The response object. Modify this object to present data to the user.

ctx.state

The custom state object that is internal to your custom handling code.

Req

This class represents the incoming request. This object is readonly.

req.client

The request client.

req.scheme

The request HTTP scheme.

req.version

The request HTTP version.

req.method

The request HTTP method.

req.path

The path of the request URL.

req.args

The args matched from url routing.

req.qs

The query string of the request URL.

req.headers

The request headers.

await req.body()

The raw request body.

await req.json()

The request's json body.

await req.form()

The request's form body.

await req.dict()

The request's json body or form body.

Res

The class represents the response returned to the frontend.

res.code

The status code.

res.headers

The response headers.

res.body

The response body.

res.json()

Response json to the frontend.

res.text()

Response text to the frontend

res.html()

Response html text to the frontend

res.redirect()

Response a redirect request to the frontend

res.file()

Response a file to the frontend

res.empty()

Empty response

State

You can attach anything to the state and fetch by key. This state shares the same state across middlewares and the route handler.

@app.get('/articles')
async def articles(ctx: Ctx) -> None:
    ctx.state.user = custom_user
    ctx.state.user # custom_user

Changelog

0.6.0 (Mar 25th, 2022)

  • Added generic response headers
  • Added wildcard URL route matching
  • Added static file serving

0.5.1 (Mar 8th, 2022)

  • Temporarily add static file

0.5.0 (Feb 24th, 2022)

  • Revert to Python implementation as the C implementation is not stable

0.2.8 (Jan 28th, 2022)

  • Fixed a bug that causes patch and delete requests to crash

0.2.7 (Jan 25th, 2022)

  • Fixed a bug that causes errors in handlers not propagated
  • Add __contains__ method to ReqHeaders

0.2.6 (Jan 25th, 2022)

  • Fixed a bug that would cause delete routes crashing

0.2.5 (Jan 24th, 2022)

  • Remove uvicorn and related dependencies
  • Rewrite with C
  • Fix req.json property

0.1.0 (Dec 10th, 2021)

  • Global methods added: get, post, patch, delete, use and apply.
  • Classes added: App, Ctx, Req, Res, State.
  • Function definition added: Handler, Next and Middleware.
  • Global helper methods added: main, gimme and make.

Metadata

Release files for thunderlight 0.6.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 thunderlight 0.6.0
File Size Uploaded Provenance Actions
thunderlight-0.6.0.tar.gz 12.2 kB Without provenance Details for thunderlight-0.6.0.tar.gz

Built distribution (wheel)

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

Total release size: 25.6 kB

Release files / thunderlight-0.6.0.tar.gz

Download URL thunderlight-0.6.0.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
781c9863f57da6f2ce8129edcb5473fee79d6eaec8ac7e603252ab1753d2085b
BLAKE2b-256 checksum
How to use checksums
7e2967156c856b208044306ebfe82e341028073b7709f7253b29b87a62a98c8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / thunderlight-0.6.0-py3-none-any.whl

Download URL thunderlight-0.6.0-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
70fb99d2dd71aee8be50ffe12006f582a0d97269885b809c792b7462ad98da3b
BLAKE2b-256 checksum
How to use checksums
1246167d600c1e8ff5c7751cd74636b02675ffbd5c503fd3cea8a490465252eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for thunderlight 0.6.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.

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