Debian Codegraph @

docs/index.rst

raw ◀ ▶
1 Debian patch touch this file
1 aioconsole
2 ==========
3
4 Asynchronous console and interfaces for asyncio
5
6 aioconsole_ provides:
7
8 * asynchronous equivalents to `input`_, `exec`_ and `code.interact`_
9 * an interactive loop running the asynchronous python console
10 * a way to customize and run command line interface using `argparse`_
11 * `stream`_ support to serve interfaces instead of using standard streams
12 * the ``apython`` script to access asyncio code at runtime without modifying the sources
13
14
15 Requirements
16 ------------
17
18 * Python \>= 3.8
19
20
21 Installation
22 ------------
23
24 aioconsole_ is available on PyPI_ and GitHub_.
25 Both of the following commands install the ``aioconsole`` package
26 and the ``apython`` script.
27
28 .. sourcecode:: console
29
30 $ pip3 install aioconsole # from PyPI
31 $ python3 setup.py install # or from the sources
32 $ apython -h
33 usage: apython [-h] [--serve [HOST:] PORT] [--no-readline]
34 [--banner BANNER] [--locals LOCALS]
35 [-m MODULE | FILE] ...
36
37
38 Asynchronous console
39 --------------------
40
41 The `example directory`_ includes a `slightly modified version`_ of the
42 `echo server from the asyncio documentation`_. It runs an echo server on
43 a given port and save the received messages in ``loop.history``.
44
45 It runs fine and doesn't use any ``aioconsole`` function:
46
47 .. sourcecode:: console
48
49 $ python3 -m example.echo 8888
50 The echo service is being served on 127.0.0.1:8888
51
52 In order to access the program while it’s running, simply replace
53 ``python3`` with ``apython`` and redirect ``stdout`` so the console is
54 not polluted by ``print`` statements (``apython`` uses ``stderr``):
55
56 .. sourcecode:: console
57
58 $ apython -m example.echo 8888 > echo.log
59 Python 3.5.0 (default, Sep 7 2015, 14:12:03)
60 [GCC 4.8.4] on linux
61 Type "help", "copyright", "credits" or "license" for more information.
62 ---
63 This console is running in an asyncio event loop.
64 It allows you to wait for coroutines using the 'await' syntax.
65 Try: await asyncio.sleep(1, result=3, loop=loop)
66 ---
67 >>>
68
69 This looks like the standard python console, with an extra message. It
70 suggests using the ``await`` syntax (``yield from`` for python 3.4):
71
72 .. sourcecode:: python3
73
74 >>> await asyncio.sleep(1, result=3, loop=loop)
75 # Wait one second...
76 3
77 >>>
78
79 The ``locals`` contain a reference to the event loop:
80
81 .. sourcecode:: python3
82
83 >>> dir()
84 ['__doc__', '__name__', 'asyncio', 'loop']
85 >>> loop
86 <InteractiveEventLoop running=True closed=False debug=False>
87 >>>
88
89 So we can access the ``history`` of received messages:
90
91 .. sourcecode:: python3
92
93 >>> loop.history
94 defaultdict(<class 'list'>, {})
95 >>> sum(loop.history.values(), [])
96 []
97
98 Let’s send a message to the server using a netcat_ client:
99
100 .. sourcecode:: console
101
102 $ nc localhost 8888
103 Hello!
104 Hello!
105
106 The echo server behaves correctly. It is now possible to retrieve the
107 message:
108
109 .. sourcecode:: python3
110
111 >>> sum(loop.history.values(), [])
112 ['Hello!']
113
114 The console also supports ``Ctrl-C`` and ``Ctrl-D`` signals:
115
116 .. sourcecode:: python3
117
118 >>> ^C
119 KeyboardInterrupt
120 >>> # Ctrl-D
121 $
122
123 All this is implemented by setting ``InteractiveEventLoop`` as default
124 event loop. It simply is a selector loop that schedules
125 ``aioconsole.interact()`` coroutine when it’s created.
126
127
128 Serving the console
129 -------------------
130
131 Moreover, ``aioconsole.interact()`` supports `stream objects`_ so it can be
132 used along with `asyncio.start\_server`_ to serve the python console.
133 The ``aioconsole.start_interactive_server`` coroutine does exactly that. A
134 backdoor can be introduced by simply adding the following line in the
135 program:
136
137 .. sourcecode:: python3
138
139 server = await aioconsole.start_interactive_server(
140 host='localhost', port=8000)
141
142 This is actually very similar to the `eventlet.backdoor module`_. It is
143 also possible to use the ``--serve`` option so it is not necessary to
144 modify the code:
145
146 .. sourcecode:: console
147
148 $ apython --serve :8889 -m example.echo 8888
149 The console is being served on 0.0.0.0:8889
150 The echo service is being served on 127.0.0.1:8888
151
152 Then connect using netcat_ and optionally, rlwrap_:
153
154 .. sourcecode:: console
155
156 $ rlwrap nc localhost 8889
157 Python 3.5.0 (default, Sep 7 2015, 14:12:03)
158 [GCC 4.8.4] on linux
159 Type "help", "copyright", "credits" or "license" for more information.
160 ---
161 This console is running in an asyncio event loop.
162 It allows you to wait for coroutines using the 'await' syntax.
163 Try: await asyncio.sleep(1, result=3, loop=loop)
164 ---
165 >>>
166
167 Great! Anyone can now forkbomb your machine:
168
169 .. sourcecode:: python3
170
171 >>> import os
172 >>> os.system(':(){ :|:& };:')
173
174
175 Command line interfaces
176 -----------------------
177
178 The package also provides an ``AsychronousCli`` object. It is
179 initialized with a dictionary of commands and can be scheduled with the
180 coroutine ``async_cli.interact()``. A dedicated command line interface
181 to the echo server is defined in `example/cli.py`_. In this case, the
182 command dictionary is defined as:
183
184 .. sourcecode:: python3
185
186 commands = {'history': (get_history, parser)}
187
188 where ``get_history`` is a coroutine and ``parser`` an `ArgumentParser`_
189 from the `argparse`_ module. The arguments of the parser will be passed
190 as keywords arguments to the coroutine.
191
192 Let’s run the command line interface:
193
194 .. sourcecode:: console
195
196 $ python3 -m example.cli 8888 > cli.log
197 Welcome to the CLI interface of echo!
198 Try:
199 * 'help' to display the help message
200 * 'list' to display the command list.
201 >>>
202
203 The ``help`` and ``list`` commands are generated automatically:
204
205 .. sourcecode:: console
206
207 >>> help
208 Type 'help' to display this message.
209 Type 'list' to display the command list.
210 Type '<command> -h' to display the help message of <command>.
211 >>> list
212 List of commands:
213 * help [-h]
214 * history [-h] [--pattern PATTERN]
215 * list [-h]
216 >>>
217
218 The ``history`` command defined earlier can be found in the list. Note
219 that it has an ``help`` option and a ``pattern`` argument:
220
221 .. sourcecode:: console
222
223 >>> history -h
224 usage: history [-h] [--pattern PATTERN]
225
226 Display the message history
227
228 optional arguments:
229 -h, --help show this help message and exit
230 --pattern PATTERN, -p PATTERN
231 pattern to filter hostnames
232
233 Example usage of the ``history`` command:
234
235 .. sourcecode:: console
236
237 >>> history
238 No message in the history
239 >>> # A few messages later
240 >>> history
241 Host 127.0.0.1:
242 0. Hello!
243 1. Bye!
244 Host 192.168.0.3
245 0. Sup!
246 >>> history -p 127.*
247 Host 127.0.0.1:
248 0. Hello!
249 1. Bye!
250
251
252 Serving interfaces
253 ------------------
254
255 Just like ``asyncio.interact()``, ``AsynchronousCli`` can be initialized
256 with any pair of `streams`_. It can be used along with
257 `asyncio.start\_server`_ to serve the command line interface. The
258 previous `example`_ provides this functionality through the
259 ``--serve-cli`` option:
260
261 .. sourcecode:: console
262
263 $ python3 -m example.cli 8888 --serve-cli 8889
264 The command line interface is being served on 127.0.0.1:8889
265 The echo service is being served on 127.0.0.1:8888
266
267 It’s now possible to access the interface using netcat_:
268
269 .. sourcecode:: console
270
271 $ rlwrap nc localhost 8889
272 Welcome to the CLI interface of echo!
273 Try:
274 * 'help' to display the help message
275 * 'list' to display the command list.
276 >>>
277
278 It is also possible to combine the example with the ``apython`` script
279 to add an extra access for debugging:
280
281 .. sourcecode:: console
282
283 $ apython --serve 8887 -m example.cli 8888 --serve-cli 8889
284 The console is being served on 127.0.0.1:8887
285 The command line interface is being served on 127.0.0.1:8889
286 The echo service is being served on 127.0.0.1:8888
287
288
289 Limitations
290 -----------
291
292 The python console exposed by `aioconsole`_ is quite limited compared to modern consoles such as `IPython`_ or `ptpython`_. Luckily, those projects gained greater asyncio support over the years. In particular, the following use cases overlap with `aioconsole`_ capabilities:
293
294 - `Embedding a ptpython console in an asyncio program <https://github.com/prompt-toolkit/ptpython/blob/master/examples/asyncio-python-embed.py>`_
295 - `Using the await syntax in an IPython console <https://ipython.readthedocs.io/en/stable/whatsnew/version7.html#autowait-asynchronous-repl>`_
296
297 Contact
298 -------
299
300 Vincent Michel: [email protected]
301
302 .. _aioconsole: https://pypi.python.org/pypi/aioconsole
303 .. _GitHub: https://github.com/vxgmichel/aioconsole
304 .. _input: https://docs.python.org/3/library/functions.html#input
305 .. _exec: https://docs.python.org/3/library/functions.html#exec
306 .. _code.interact: https://docs.python.org/2/library/code.html#code.interact
307 .. _argparse: https://docs.python.org/dev/library/argparse.html
308 .. _stream: https://docs.python.org/3.4/library/asyncio-stream.html
309 .. _example directory: https://github.com/vxgmichel/aioconsole/blob/master/example
310 .. _example/echo.py: https://github.com/vxgmichel/aioconsole/blob/master/example/echo.py
311 .. _echo server from the asyncio documentation: https://docs.python.org/3/library/asyncio-stream.html#tcp-echo-server-using-streams
312 .. _asyncio.start\_server: https://docs.python.org/3.4/library/asyncio-stream.html#asyncio.start_server
313 .. _eventlet.backdoor module: http://eventlet.readthedocs.io/en/latest/modules/backdoor.html#backdoor-python-interactive-interpreter-within-a-running-process
314 .. _example/cli.py: https://github.com/vxgmichel/aioconsole/blob/master/example/cli.py
315 .. _ArgumentParser: https://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser
316
317 .. _streams: stream_
318 .. _stream objects: stream_
319 .. _slightly modified version: `example/echo.py`_
320 .. _example: `example/cli.py`_
321 .. _PyPI: aioconsole_
322 .. _netcat: https://linux.die.net/man/1/nc
323 .. _rlwrap: https://linux.die.net/man/1/rlwrap
324 .. _IPython: https://ipython.readthedocs.io
325 .. _ptpython: https://github.com/prompt-toolkit/ptpython