The web server

ASAB provides a web server in a asab.web module. This module offers an integration of a aiohttp web server.

  1. Before you start, make sure that you have aiohttp module installed.
$ pip3 install aiohttp
  1. The following code creates a simple web server application
#!/usr/bin/env python3
import asab
import asab.web
import aiohttp

class MyApplication(asab.Application):

    def __init__(self):
        super().__init__()

        # Load the ASAB Web module
        self.add_module(asab.web.Module)

        # Locate the ASAB Web service
        websvc = self.get_service("asab.WebService")

        # Create the Web container
        container = asab.web.WebContainer(websvc, 'my:web', config={"listen": "0.0.0.0:8080"})

        # Add a route to the handler
        container.WebApp.router.add_get('/hello', self.hello)

    # This is the web request handler
    async def hello(self, request):
        return aiohttp.web.Response(text='Hello!\n')

if __name__ == '__main__':
    app = MyApplication()
    app.run()
  1. Test it with curl
$ curl http://localhost:8080/hello
Hello!

Web Service

class asab.web.service.WebService

Service location example:

from asab.web import Module
self.add_module(Module)
svc = self.get_service("asab.WebService")

Configuration

The default configuration of the web container in ASAB is following:

[web]
listen=0.0.0.0:8080

Multiple listening interfaces can be specified:

[web]
listen:
        0.0.0.0:8080
        :: 8080

Multiple listening interfaces, one with HTTPS (TLS/SSL) can be specified:

[web]
listen:
        0.0.0.0 8080
        :: 8080
        0.0.0.0 8443 ssl:web

[ssl:web]
cert=...
key=...
...

Multiple interfaces, one with HTTPS (inline):

[web]
listen:
        0.0.0.0 8080
        :: 8080
        0.0.0.0 8443 ssl

# The SSL parameters are inside of the WebContainer section
cert=...
key=...
...

Other available options are:

  • backlog
  • rootdir
  • servertokens (default value full)
  • cors
  • cors_preflight_paths

TLS/SSL paramereters:

  • cert
  • key
  • password
  • cafile
  • capath
  • ciphers
  • dh_params
  • verify_mode: one of CERT_NONE, CERT_OPTIONAL or CERT_REQUIRED
  • check_hostname
  • options

Sessions

ASAB Web Service provides an implementation of the web sessions.

class asab.web.session.ServiceWebSession

TODO: …

asab.web.session.session_middleware(storage)

TODO: …