Skip to content

Kathara Python API

Tommaso Caiazzi edited this page May 24, 2023 · 6 revisions

Kathará provides also Python APIs that allow you to interact with it within Python apps.

Installation

The latest stable version is available on PyPI.

python3 -m pip install "kathara[pyuv]"

⚠️ WARNING: [pyuv] is required to install pyuv as an extra dependency, since it may be blocked by PyPI from being downloaded from a git repository, giving this error:

Packages installed from PyPI cannot depend on packages which are not also hosted on PyPI.

If you are getting this error, first install Kathará without [pyuv] and then re-execute the command with the extra dependency:

python3 -m pip install kathara
python3 -m pip install "kathara[pyuv]"

If the problem persists, install pyuv manually and then install Kathará:

python3 -m pip install git+https://github.com/saghul/pyuv@master#egg=pyuv
python3 -m pip install kathara

Getting Started

To interact with Kathará, you first need to create a network scenario.

from Kathara.model.Lab import Lab
from Kathara.manager.Kathara import Kathara

lab = Lab("kathara network scenario")

You can now create devices.

pc1 = lab.new_machine("pc1")
pc2 = lab.new_machine("pc2")

You can connect them.

lab.connect_machine_to_link(pc1.name, "A")
lab.connect_machine_to_link(pc2.name, "A")

Now you are ready to deploy the network scenario.

Kathara.get_instance().deploy_lab(lab)

You can check that the devices are up and running.

print(next(Kathara.get_instance().get_machines_stats(lab_name=lab.name)))

This is the output.

{'kathara_user-cfp7yzdwcy6z7geo1lgx7g_pc1_j7CX6Y6grTLV4tO63dCvXw': {'network_scenario_id': 'j7CX6Y6grTLV4tO63dCvXw', 'name': 'pc1', 'container_name': 'kathara_user-cfp7yzdwcy6z7geo1lgx7g_pc1_j7CX6Y6grTLV4tO63dCvXw', 'user': 'user-cfp7yzdwcy6z7geo1lgx7g', 'status': 'running', 'image': 'kathara/quagga:latest', 'pids': 1, 'cpu_usage': '0.00%', 'mem_usage': '916.0 KB / 15.37 GB', 'mem_percent': '0.01 %', 'net_usage': '572.0 B / 0 B'}, 'kathara_user-cfp7yzdwcy6z7geo1lgx7g_pc2_j7CX6Y6grTLV4tO63dCvXw': {'network_scenario_id': 'j7CX6Y6grTLV4tO63dCvXw', 'name': 'pc2', 'container_name': 'kathara_user-cfp7yzdwcy6z7geo1lgx7g_pc2_j7CX6Y6grTLV4tO63dCvXw', 'user': 'user-cfp7yzdwcy6z7geo1lgx7g', 'status': 'running', 'image': 'kathara/quagga:latest', 'pids': 1, 'cpu_usage': '0.00%', 'mem_usage': '916.0 KB / 15.37 GB', 'mem_percent': '0.01 %', 'net_usage': '2.55 KB / 0 B'}}

This is just an example of what you can do with Kathará Python API.

For more information, see the docs.

Clone this wiki locally