aiocoap.util.cli module

Helpers for creating server-style applications in aiocoap

Note that these are not particular to aiocoap, but are used at different places in aiocoap and thus shared here.

class aiocoap.util.cli.ActionNoYes(option_strings, dest, default=True, required=False, help=None)

Bases: argparse.Action

Simple action that automatically manages –{,no-}something style options

class aiocoap.util.cli.AsyncCLIDaemon(*args, **kwargs)

Bases: object

Helper for creating daemon-style CLI prorgrams.

Note that this currently doesn’t create a Daemon in the sense of doing a daemon-fork; that could be added on demand, though.

Subclass this and implement the start() method as an async function; it will be passed all the constructor’s arguments.

When all setup is complete and the program is operational, return from the start method.

Implement the shutdown() coroutine and to do cleanup; what actually runs your program will, if possible, call that and await its return.

Two usage patterns for this are supported:

  • Outside of an async context, run run MyClass.sync_main(), typically in the program’s if __name__ == "__main__": section.

    In this mode, the loop that is started is configured to safely shut down the loop when SIGINT is received.

  • To run a subclass of this in an existing loop, start it with MyClass(...) (possibly passing in the loop to run it on if not already in an async context), and then awaiting its .initializing future. To stop it, await its .shutdown() method.

    Note that with this usage pattern, the stop() method has no effect; servers that .stop() themselves need to signal their desire to be shut down through other channels (but that is an atypical case).


Stop the operation (and exit sync_main) at the next convenience.

classmethod sync_main(*args, **kwargs)

Run the application in an AsyncIO main loop, shutting down cleanly on keyboard interrupt.