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[source]

Service localization example:

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

An instance of a aiohttp.web.Application class.

svc.WebApp.router.add_get('/hello', self.hello)

Configuration

TODO: Listen at 0.0.0.0:80

Sessions

ASAB Web Service provides an implementation of the web sessions.

class asab.web.session.ServiceWebSession[source]

TODO: …

asab.web.session.session_middleware(storage)[source]

TODO: …