bonzo.server – Non-blocking SMTP server¶
A non-blocking, single-threaded SMTP server.
- class bonzo.server.SMTPConnection(stream, address, request_callback)[source]¶
Bases:
objectHandles a connection to an SMTP client, executing SMTP commands.
This class uses its
COMMANDandDATAattributes as a simple “enum” to manage the connection state.- COMMAND = 0¶
Used to set the state to receive any command.
- DATA = 1¶
Used to set the state to receive data.
- class bonzo.server.SMTPRequest(connection, remote_ip, command, hostname=None, mail=None, rcpt=None, data=None)[source]¶
Bases:
objectA single SMTP request.
- property message¶
Returns an instance of a subclass from the
email.mime.base.MIMEBaseclass. It’s actually parsed from the data received using themessage_from_string()method.
- class bonzo.server.SMTPServer(request_callback, **kwargs)[source]¶
Bases:
TCPServerA non-blocking, single-threaded SMTP server.
A server is defined by a request callback that takes an instance of
SMTPRequestas an argument.A simple example server that handles the request with the received message:
import asyncio from bonzo.server import SMTPServer async def handle_request(request): do_something_with_the_message(request.message) await request.finish_async() async def main(): smtp_server = SMTPServer(handle_request) smtp_server.listen(2525) await asyncio.Event().wait() asyncio.run(main())