/usr/lib/python3/dist-packages/lazr/smtptest/docs/queue.rst is in python3-lazr.smtptest 2.0.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | ============
Queue server
============
As shown in the general usage_ document, you can create subclasses of the
Server class to define how you want to handle received messages. A very
common pattern is to use a Queue to share messages between the controlling
thread and the server thread. These are nice because, unlike a mailbox based
server, no sorting is required to get the messages out of the server in the
same order they are sent.
.. _usage: usage.html
First, you need a queue.
>>> try:
... from queue import Empty, Queue
... except ImportError:
... # Python 2
... from Queue import Empty, Queue
>>> queue = Queue()
Then you need a server.
>>> from lazr.smtptest.server import QueueServer
>>> server = QueueServer('localhost', 9025, queue)
Finally, you need a controller.
>>> from lazr.smtptest.controller import Controller
>>> controller = Controller(server)
Now, start the SMTP server.
>>> controller.start()
Send the server a bunch of messages.
>>> import smtplib
>>> smtpd = smtplib.SMTP()
>>> code, helo = smtpd.connect('localhost', 9025)
>>> print(code, str(helo))
220 ... Python SMTP proxy version ...
>>> smtpd.sendmail('iperson@example.com', ['jperson@example.com'], """\
... From: Irie Person <iperson@example.com>
... To: Jeff Person <jperson@example.com>
... Subject: A test
... Message-ID: <elephant>
...
... This is a test.
... """)
{}
>>> smtpd.sendmail('kperson@example.com', ['lperson@example.com'], """\
... From: Kari Person <kperson@example.com>
... To: Liam Person <lperson@example.com>
... Subject: A test
... Message-ID: <falcon>
...
... This is a test.
... """)
{}
>>> smtpd.sendmail('mperson@example.com', ['nperson@example.com'], """\
... From: Mary Person <mperson@example.com>
... To: Neal Person <nperson@example.com>
... Subject: A test
... Message-ID: <goat>
...
... This is a test.
... """)
{}
All of these messages are available in the queue.
>>> while True:
... try:
... message = queue.get_nowait()
... except Empty:
... break
... print(message['message-id'])
<elephant>
<falcon>
<goat>
We're done with the controller.
>>> controller.stop()
Queue controller
================
An even more convenient interface, is to use the QueueController.
>>> from lazr.smtptest.controller import QueueController
>>> controller = QueueController('localhost', 9025)
>>> controller.start()
>>> smtpd = smtplib.SMTP()
>>> code, helo = smtpd.connect('localhost', 9025)
>>> print(code, str(helo))
220 ... Python SMTP proxy version ...
We now have an SMTP server that we can send some messages to.
>>> smtpd.sendmail('operson@example.com', ['pperson@example.com'], """\
... From: Onua Person <operson@example.com>
... To: Paul Person <pperson@example.com>
... Subject: A test
... Message-ID: <horse>
...
... This is a test.
... """)
{}
>>> smtpd.sendmail('qperson@example.com', ['rperson@example.com'], """\
... From: Quay Person <qperson@example.com>
... To: Raul Person <rperson@example.com>
... Subject: A test
... Message-ID: <iguana>
...
... This is a test.
... """)
{}
>>> smtpd.sendmail('sperson@example.com', ['tperson@example.com'], """\
... From: Sean Person <sperson@example.com>
... To: Thom Person <tperson@example.com>
... Subject: A test
... Message-ID: <jackel>
...
... This is a test.
... """)
{}
And we can dump out all the messages from the controller.
>>> for message in controller:
... print(message['message-id'])
<horse>
<iguana>
<jackel>
We can send more messages and view them too.
>>> smtpd.sendmail('uperson@example.com', ['vperson@example.com'], """\
... From: Umma Person <uperson@example.com>
... To: Vern Person <vperson@example.com>
... Subject: A test
... Message-ID: <kangaroo>
...
... This is a test.
... """)
{}
>>> for message in controller:
... print(message['message-id'])
<kangaroo>
Resetting
=========
Queue servers support a RSET (reset) method, which empties the queue.
>>> smtpd.sendmail('wperson@example.com', ['xperson@example.com'], """\
... From: Wynn Person <wperson@example.com>
... To: Xerx Person <xperson@example.com>
... Subject: A test
... Message-ID: <llama>
...
... This is a test.
... """)
{}
>>> smtpd.sendmail('yperson@example.com', ['zperson@example.com'], """\
... From: Yikes Person <yperson@example.com>
... To: Zell Person <zperson@example.com>
... Subject: A test
... Message-ID: <moose>
...
... This is a test.
... """)
{}
>>> controller.queue.qsize()
2
>>> controller.reset()
>>> controller.queue.qsize()
0
Clean up
========
We're done with this controller.
>>> controller.stop()
|