Pyjo.Server - HTTP/WebSocket server base class

from Pyjo.Server import Server

class Pyjo_Server_MyServer(Server):
    def run(self):
        # Get a transaction
        tx = self.build_tx()

        # Emit "request" event
        self.emit('request', tx)

# Or import the module, as in Google Python Style Guide

import Pyjo.Server

class Pyjo_Server_MyServer(Pyjo.Server.object):
    def run(self):
        # Get a transaction
        tx = self.build_tx()

        # Emit "request" event
        self.emit('request', tx)

Pyjo.Server is an abstract base class for HTTP/WebSocket servers and server interfaces, like Pyjo.Server.ASGI, Pyjo.Server.CGI, Pyjo.Server.Daemon and Pyjo.Server.WSGI.

A server of another distribution is a module in the namespace Pyjo.Server, which pkgutil.extend_path() keeps open, as for Pyjo.Reactor.

Events

Pyjo.Server inherits all events from Pyjo.EventEmitter and can emit the following new ones.

request

server.on('request', lambda server, tx: ...)

Emitted when a request is ready and needs to be handled.

@server.on('request')
def request(server, tx):
    tx.res.code = 200
    tx.res.headers.content_type = 'text/plain'
    tx.res.body = b'Hello World!'
    tx.resume()

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Server of Mojolicious 9.49.

Classes

class Pyjo.Server.Application(*args, **kwargs)[source]

What a server needs from the application it handles.

class Pyjo.Server.Pyjo_Server(*, app: Application | None = None, reverse_proxy: bool | None = None, trusted_proxies: list[str] | None = None)[source]

Pyjo.Server inherits all methods from Pyjo.EventEmitter and implements the following new ones.

property app: Application[source]
app = server.app
server.app = MyApp.new()

Application this server handles, defaults to a Pyjo.HelloWorld object.

build_app(app: str | type, **kwargs: Any) → Application[source]
app = server.build_app('MyApp')
app = server.build_app('MyApp', log=Pyjo.Log.new())
app = server.build_app(MyApp)

Build application from class and assign it to app. The class is a dotted name of a module with a new function, as the modules of Pyjoyment have, or of a class in a module, or the class itself.

build_tx() → Pyjo_Transaction_HTTP[source]
tx = server.build_tx()

Let application build a transaction.

daemonize() → None[source]
server.daemonize()

Daemonize server process.

load_app(path: str | PathLike[str], **kwargs: Any) → Application[source]
app = server.load_app('/home/sri/myapp.py')
app = server.load_app('/home/sri/myapp.py', log=Pyjo.Log.new())

Load application from script and assign it to app. The script starts its application, as MyApp.new().start(), and the start method keeps it in loaded_app instead of running a command, as the environment variable PYJO_APP_LOADER is set.

print(Pyjo.Server.new().load_app('./myapp.py').log.level)
property reverse_proxy: bool[source]
boolean = server.reverse_proxy
server.reverse_proxy = boolean

This server operates behind a reverse proxy, defaults to the value of the PYJO_REVERSE_PROXY environment variable or true if trusted_proxies is not empty.

run() → Anything[source]
server.run()

Run server. Meant to be overloaded in a subclass.

property trusted_proxies: list[str][source]
proxies = server.trusted_proxies
server.trusted_proxies = ['10.0.0.0/8', '127.0.0.1', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7']

This server expects requests from trusted reverse proxies, defaults to the value of the PYJO_TRUSTED_PROXIES environment variable split on commas with optional whitespace. These proxies should be addresses or networks in CIDR form.

Pyjo.Server.args_override: dict[str, Any] = {}

The arguments of load_app() for the constructor of the application that the script builds.

Pyjo.Server.loaded_app: Application | None = None

The application that the script of load_app() starts: its start keeps itself here instead of running a command, when the environment variable PYJO_APP_LOADER is set.