Asynchronous library for working with the Mikrotik API

Strongly welcome.

Recently, I needed to work with Mikrotik through its API. It seems to be nothing remarkable, there is an official library , there is also a wrapper on the github , but here's the trouble - I had to work asynchronously through asyncio and using async / await buns. And I did not find such a library.

I had to write myself.

The article will not be very long, because there is nothing special to write about. And a link to the repository would be quite enough .

Package Installation:

pip install aio_api_ros

Here is a usage example:

import asyncio
from aio_api_ros import create_rosapi_connection
async def main():
    # устанавливаем коннект
    mk = await create_rosapi_connection(
        mk_ip='127.0.0.1',
        mk_port=8728,
        mk_user='myuser',
        mk_psw='mypassword'
    )
    # отправляем команду
    mk.talk_word('/ip/hotspot/active/print')
    # считываем ответ от микротика
    res = await mk.read()
    print(res)
    mk.close()
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()

It is also possible to create a simple pool of connections before Mikrotik, but the pool was not tested in combat conditions.

The same example but with a pool of connections
import asyncio
from aio_api_ros import create_rosapi_simple_pool
async def main():
    mk = await create_rosapi_simple_pool(
        mk_ip='127.0.0.1',
        mk_port=8728,
        mk_user='myuser',
        mk_psw='mypassword',
        max_size=4
    )
    await mk.talk_word('/ip/hotspot/active/print')
    res = await mk.read()
    print(res)
    mk.close()
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
    loop.close()


For parsing responses from the microt, I took advantage of the developments from this repository.

I hope this is useful to someone.

Also popular now: