Aeros.WebServer

This module contains the main web server class

Module Contents

Classes

WebServer

This is the main server class which extends a standard Flask class by a bunch of features and major

class Aeros.WebServer.WebServer(import_name: str, host: str = '0.0.0.0', port: int = 80, include_server_header: bool = True, logging_level: int = INFO, cache: Aeros.caching.server.Cache = None, compression: Aeros.compression.Base = None, global_headers: dict = {}, *args, **kwargs)[source]

Bases: quart.Quart

This is the main server class which extends a standard Flask class by a bunch of features and major performance improvements. It extends the quart class, which by itself is already an enhanced version of the Flask class. This class however allows production-grade deployment using the uvicorn ASGI server as production server. But instead of calling the uvicorn command via the console, it can be started directly from the Python code itself, making it easier to integrate in higher-level scripts and applications without calling os.system() or subprocess.Popen().

__log_format_std = [%(asctime)s] %(levelname)s: %(message)s
cache(self, timeout=None, key_prefix='view/%s', unless=None, forced_update=None, response_filter=None, query_string=False, hash_method=hashlib.md5, cache_none=False)[source]

A simple wrapper that forwards the cached() decorator to the internal Cache() instance. May be used as the normal @cache.cached() decorator.

clear_cache(self)

Clears the WebServer instance cache. Use with caution!

_get_own_instance_path(self)str[source]

Retrieves the file and variable name of this instance to be used in the uvicorn CLI.

Since uvicorn needs the application’s file and global variable name for multicore execution, an instance needs to know it’s own origin file and name. But since this class is not defined in the same file as it is called or defined from, this method searches for the correct module/file and evaluates it’s instance name.

Warning

This method is a beta feature. Use with caution (good testing).

run_server(self, host: str = None, port: int = None, log_level: int = None, access_log_file: str = None, access_log_to_std: bool = True, traceback: bool = True, soft_limit: int = 32, hard_limit: int = 42, **kwargs)None[source]

Generates the necessary config and runs the server instance. Possible keyword arguments are the same as supported by the uvicorn.run() method as they are passed into Config(app, **kwargs). This method also configures features like caching and compression that are not default in Quart or Flask and unique to Aeros or require third-party modules to be configured.

run(self, *args, **kwargs)

Run this application.

This is best used for development only, see Hypercorn for production servers.

Parameters
  • host – Hostname to listen on. By default this is loopback only, use 0.0.0.0 to have the server listen externally.

  • port – Port number to listen on.

  • debug – If set enable (or disable) debug mode and debug output.

  • use_reloader – Automatically reload on code changes.

  • loop – Asyncio loop to create the server in, if None, take default one. If specified it is the caller’s responsibility to close and cleanup the loop.

  • ca_certs – Path to the SSL CA certificate file.

  • certfile – Path to the SSL certificate file.

  • keyfile – Path to the SSL key file.

route(self, *args, **kwargs)

Add a route to the application.

This is designed to be used as a decorator, if used to decorate a synchronous function, the function will be wrapped in run_sync() and run in a thread executor (with the wrapped function returned). An example usage,

@app.route('/')
async def route():
    ...
Parameters
  • rule – The path to route on, should start with a /.

  • methods – List of HTTP verbs the function routes.

  • endpoint – Optional endpoint name, if not present the function name is used.

  • defaults

    A dictionary of variables to provide automatically, use to provide a simpler default path for a route, e.g. to allow for /book rather than /book/0,

    @app.route('/book', defaults={'page': 0})
    @app.route('/book/<int:page>')
    def book(page):
        ...
    

  • host – The full host name for this route (should include subdomain if needed) - cannot be used with subdomain.

  • subdomain – A subdomain for this specific route.

  • provide_automatic_options – Optionally False to prevent OPTION handling.

  • strict_slashes – Strictly match the trailing slash present in the path. Will redirect a leaf (no slash) to a branch (with slash).