bonzo.smtpRequestHandler and Application classes

Tools for handling requests with asynchronous features.

class bonzo.smtp.Application(handler_class, **settings)[source]

Bases: object

Instances of this class are callable and can be passed directly to SMTPServer to handle messages:

async def main():
    application = smtp.Application(Handler)
    smtp_server = server.SMTPServer(application)
    smtp_server.listen(2525)
    await asyncio.Event().wait()
settings

Additional keyword arguments passed to the constructor are saved in the settings dictionary, and are often referred to in documentation as “application settings”. Settings are used to customize various aspects of Bonzo (although in some cases richer customization is possible by overriding methods in a subclass of RequestHandler). Some applications also like to use the settings dictionary as a way to make application-specific settings available to handlers without using global variables.

listen(port, address='', **kwargs)[source]

Starts an SMTP server for this handler on the given port.

This is a convenience alias for creating an SMTPServer object and calling its listen method. Keyword arguments not supported by SMTPServer.listen are passed to the SMTPServer constructor. For advanced uses (e.g. multi-process mode), do not use this method; create an SMTPServer and call its bind()/ start() methods directly.

Note that after calling this method you still need to run the active asyncio event loop to start the server.

class bonzo.smtp.RequestHandler(application, request)[source]

Bases: object

Subclass this class and define data() to make a handler.

data()[source]
finish()[source]

Finishes this response, ending the SMTP request.

async finish_async()[source]

Finishes this response, ending the SMTP request.

on_connection_close()[source]

Called in async handlers if the client closed the connection.

on_finish()[source]

Called after the end of a request.

Override this method to perform cleanup, logging, etc. This method is a counterpart to prepare(). on_finish may not produce any output, as it is called after the response has been sent to the client.

prepare()[source]

Called at the beginning of a request before data().

Override this method to perform common initialization regardless of the request method.

property settings

An alias for self.application.settings.