Skip to main content

A lightweight ASGI framework

Project description

bareASGI

A lightweight ASGI framework (read the documentation)

Status

Work in progress.

Overview

This is a bare ASGI web server framework. The goal is to provide a minimal implementation, with other facilities (serving static files, CORS, sessions, etc.) being implemented by optional packages.

Some of the features provided by web frameworks are not required for a given app, or conflict with the version or varient required for a given solution.

See also:

Functionality

The framework supports:

  • Http,
  • WebSockets,
  • A basic method and path based router,
  • Middleware.

Examples

These examples use uvicorn as the ASGI server.

Simple Client

Here is a simple example which returns some text.

import uvicorn
from bareasgi import Application, text_writer

async def http_request_callback(scope, info, matches, content):
    return 200, [(b'content-type', b'text/plain')], text_writer('This is not a test')

app = Application()
app.http_router.add({'GET'}, '/{rest:path}', http_request_callback)

uvicorn.run(app, port=9009)

Rest Server

Here is a simple rest server.

import uvicorn
import json
from bareasgi import Application, text_reader, text_writer

async def get_info(scope, info, matches, content):
    text = json.dumps(info)
    return 200, [(b'content-type', b'application/json')], text_writer(text)

async def set_info(scope, info, matches, content):
    text = await text_reader(content)
    data = json.loads(text)
    info.update(data)
    return 204, None, None

app = Application(info={'name': 'Michael Caine'})
app.http_router.add({'GET'}, '/info', get_info)
app.http_router.add({'POST'}, '/info', set_info)

uvicorn.run(app, port=9009)

WebSockets

A WebSocket example can be found in the examples folder. Here is the handler.

async def test_callback(scope, info, matches, web_socket):
    await web_socket.accept()

    try:
        while True:
            text = await web_socket.receive()
            if text is None:
                break
            await web_socket.send('You said: ' + text)
    except Exception as error:
        print(error)

    await web_socket.close()

Middleware

Here is a simple middleware example.

import uvicorn
from bareasgi import Application, text_writer

async def first_middleware(scope, info, matches, content, handler):
    info['message'] = 'This is first the middleware. '
    response = await handler(scope, info, matches, content)
    return response


async def second_middleware(scope, info, matches, content, handler):
    info['message'] += 'This is the second middleware.'
    response = await handler(scope, info, matches, content)
    return response


async def http_request_callback(scope, info, matches, content):
    return 200, [(b'content-type', b'text/plain')], text_writer(info['message'])


app = Application(middlewares=[first_middleware, second_middleware])
app.http_router.add({'GET'}, '/test', http_request_callback)

uvicorn.run(app, port=9009)

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

bareasgi-2.0.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

bareasgi-2.0.1-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file bareasgi-2.0.1.tar.gz.

File metadata

  • Download URL: bareasgi-2.0.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for bareasgi-2.0.1.tar.gz
Algorithm Hash digest
SHA256 ef8d6e0649b4d2d43c730a532539dcff4edc6b2425e1c143aefca48827c047ce
MD5 dfec41c4d7f5c83586c948cd20f5c0ab
BLAKE2b-256 a58e04abe096be0927d11e45ef3f3bec0d648b34a365c59e689b08a147c3afb9

See more details on using hashes here.

File details

Details for the file bareasgi-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: bareasgi-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for bareasgi-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 05cf33f34b9906592b5ce00f97bbd1099f6f6de6dd8afd77e6019c14557257b3
MD5 b81b2758e600d25d06beec3c0916c2e1
BLAKE2b-256 ecdecaf83cb212c7b42aeb46b28594c0f9d45fa411d4cbf18fe41fa8441393d1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page