Skip to main content

Python bindings for libknot

Project description

Libknot API in Python

A Python interface for managing the Knot DNS daemon.

Table of contents

Introduction

If the shared libknot.so library isn't available in the library search path, it's necessary to load the library first, e.g.:

import libknot
libknot.Knot("/usr/lib/libknot.so")

Control module

Using this module it's possible to create scripts for efficient tasks that would require complex shell scripts with multiple calls of knotc. For communication with the daemon it uses the same mechanism as the knotc utility, i.e. communication via a Unix socket.

The module API is stored in libknot.control.

Using the Control module

The module usage consists of several steps:

  • Initialization and connection to the daemon control socket.
  • One or more control operations. An operation is called by sending a command with optional data to the daemon. The operation result has to be received afterwards.
  • Closing the connection and deinitialization.

Control module example

import json
import libknot.control

# Initialization
ctl = libknot.control.KnotCtl()
ctl.connect("/var/run/knot/knot.sock")
ctl.set_timeout(60)

try:
    # Operation without parameters
    ctl.send_block(cmd="conf-begin")
    resp = ctl.receive_block()

    # Operation with parameters
    ctl.send_block(cmd="conf-set", section="zone", item="domain", data="test")
    resp = ctl.receive_block()

    ctl.send_block(cmd="conf-commit")
    resp = ctl.receive_block()

    # Operation with a result displayed in JSON format
    ctl.send_block(cmd="conf-read", section="zone", item="domain")
    resp = ctl.receive_block()
    print(json.dumps(resp, indent=4))
except libknot.control.KnotCtlError as exc:
    # Print libknot error
    print(exc)
finally:
    # Deinitialization
    ctl.send(libknot.control.KnotCtlType.END)
    ctl.close()
    # List configured zones (including catalog member ones)
    ctl.send_block(cmd="conf-list", flags="z")
    resp = ctl.receive_block()
    for zone in resp['zone']:
        print(zone)

Probe module

Using this module it's possible to receive traffic data from a running daemon with active probe module.

The module API is stored in libknot.probe.

Using the Probe module

The module usage consists of several steps:

  • Initialization of one or more probe channels
  • Periodical receiving of data units from the channels and data processing

Probe module example

import libknot.probe

# Initialization of the first probe channel stored in `/run/knot`
probe = libknot.probe.KnotProbe("/run/knot", 1)

# Array for storing up to 8 data units
data = libknot.probe.KnotProbeDataArray(8)
while (True):
    # Receiving data units with timeout of 1000 ms
    if probe.consume(data, 1000) > 0:
        # Printing received data units in the default format
        for item in data:
            print(item)

Dname module

This module provides a few dname-related operations.

Using the Dname module

The dname object is initialized from a string with textual dname. Then the dname can be reformatted to wire format or back to textual format.

Dname module example

import libknot.dname

dname1 = libknot.dname.KnotDname("knot-dns.cz")
print("%s: wire: %s size: %u" % (dname1.str(), dname1.wire(), dname1.size()))

dname2 = libknot.dname.KnotDname("e\\120ample.c\om.")
print("%s: wire: %s size: %u" % (dname2.str(), dname2.wire(), dname2.size()))

dname3 = libknot.dname.KnotDname(dname_wire=b'\x02cz\x00')
print("%s: wire: %s size: %u" % (dname3.str(), dname3.wire(), dname3.size()))
knot-dns.cz.: wire: b'\x08knot-dns\x02cz\x00' size: 13
example.com.: wire: b'\x07example\x03com\x00' size: 13
cz.: wire: b'\x02cz\x00' size: 4

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

libknot-3.3.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

libknot-3.3.1-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file libknot-3.3.1.tar.gz.

File metadata

  • Download URL: libknot-3.3.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for libknot-3.3.1.tar.gz
Algorithm Hash digest
SHA256 ce8683cea4e3df5c07b060b3fd0c3fd89d1b43d0ad916aa54876f8cbe255f14f
MD5 6b82527772d7e38cdf7b43afc9e39940
BLAKE2b-256 1fdfe4ef8df3921321b08386c6e789581ba98c3a75ab62531799f3cb0cac6ba7

See more details on using hashes here.

File details

Details for the file libknot-3.3.1-py3-none-any.whl.

File metadata

  • Download URL: libknot-3.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for libknot-3.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ede903e6953f94d8c3df03ddbb4afc46c56d599642d283268d9301f164a5cbee
MD5 7f3a9332562c37a8f7c0f0143ceb7691
BLAKE2b-256 0fec3dceb720d08e53692eaaeddd9cd2132e4864c29b5401a96a28d0bec67a1a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page