Package: libknot
Libknot API in PythonA Python interface for managing the Knot DNS daemon.Table of contentsIntroductionControl moduleUsageExampleProbe moduleUsageExampleDname moduleUsageExampleIntroductionIf the sharedlibknot.solibrary isn't available in the library search path, it's
necessary to load the library first, e.g.:importlibknotlibknot.Knot("/usr/lib/libknot.so")Control moduleUsing this module it's possible to create scripts for efficient tasks that
would require complex shell scripts with multiple calls ofknotc. For
communication with the daemon it uses the same mechanism as theknotcutility,
i.e. communication via a Unix socket.The module API is stored inlibknot.control.Using the Control moduleThe 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 exampleimportjsonimportlibknot.control# Initializationctl=libknot.control.KnotCtl()ctl.connect("/var/run/knot/knot.sock")ctl.set_timeout(60)try:# Operation without parametersctl.send_block(cmd="conf-begin")resp=ctl.receive_block()# Operation with parametersctl.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 formatctl.send_block(cmd="conf-read",section="zone",item="domain")resp=ctl.receive_block()print(json.dumps(resp,indent=4))exceptlibknot.control.KnotCtlErrorasexc:# Print libknot errorprint(exc)finally:# Deinitializationctl.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()forzoneinresp['zone']:print(zone)# Print expirations as unixtime for all secondary zonesctl.send_block(cmd="zone-status",flags="u")resp=ctl.receive_block()forzoneinresp:ifresp[zone]["role"]=="master":continueexpiration=resp[zone]["expiration"]ifexpiration=="-":print("Zone%snot loaded"%zone)else:print("Zone%sexpires at%s"%(zone,resp[zone]["expiration"]))Probe moduleUsing this module it's possible to receive traffic data from a running daemon with
active probe module.The module API is stored inlibknot.probe.Using the Probe moduleThe module usage consists of several steps:Initialization of one or more probe channelsPeriodical receiving of data units from the channels and data processingProbe module exampleimportlibknot.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 unitsdata=libknot.probe.KnotProbeDataArray(8)while(True):# Receiving data units with timeout of 1000 msifprobe.consume(data,1000)>0:# Printing received data units in the default formatforitemindata:print(item)Dname moduleThis module provides a few dname-related operations.Using the Dname moduleThe 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 exampleimportlibknot.dnamedname1=libknot.dname.KnotDname("knot-dns.cz")print("%s: wire:%ssize:%u"%(dname1.str(),dname1.wire(),dname1.size()))dname2=libknot.dname.KnotDname("e\\120ample.c\om.")print("%s: wire:%ssize:%u"%(dname2.str(),dname2.wire(),dname2.size()))dname3=libknot.dname.KnotDname(dname_wire=b'\x02cz\x00')print("%s: wire:%ssize:%u"%(dname3.str(),dname3.wire(),dname3.size()))knot-dns.cz.:wire:b'\x08knot-dns\x02cz\x00'size:13example.com.:wire:b'\x07example\x03com\x00'size:13cz.:wire:b'\x02cz\x00'size:4