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