Python Package
We provide a Python binding of our Native Shared C Library, allowing you to use drasyl in a Pythonic as possible.
Installationβ
We provide our binding as a Python package hosted on Python Package Index. Therefore you can install it simply by running the following command:
pip install drasyl
Usageβ
First, we have to initialize a drasyl node and then retrieve the node's identity (please refer to the Getting Started section first, if you're new to working with our DrasylNode
):
from drasyl import *
import time
def on_event(event):
# code
drasyl_node_init("my-node.conf", on_event)
identity = drasyl_node_identity()
print("My address: %s" % identity.identity_public_key)
It is now time to start the node, wait for it to come online, and then send a message to a peer (keep in mind that you need to adjust the recipient address to a running node. Otherwise, the send will fail):
from drasyl import *
import time
# code
drasyl_node_start()
print("Wait for node to become online...")
while not drasyl_node_is_online():
time.sleep(0.05)
recipient = "78483253e5dbbe8f401dd1bd1ef0b6f1830c46e411f611dc93a664c1e44cc054".encode("UTF-8")
payload = "hello there".encode("UTF-8")
drasyl_node_send(recipient, payload)
If the node is no longer needed, it can be shut down. Finally, we need to shut down all threads implicitly spawned by drasyl and tear down the isolate:
from drasyl import *
import time
# code
drasyl_node_stop()
drasyl_shutdown_event_loop()
thread_tear_down()
Referenceβ
drasyl_node_versionβ
def drasyl_node_version()
Returns the version of the drasyl node currently loaded.
drasyl_set_loggerβ
def drasyl_set_logger(logger)
Sets logger callback function.
Parametersβ
logger
: Must have the following signature:def console_logger(level, time, message)
.
drasyl_node_initβ
def drasyl_node_init(config, listener)
Creates a new DrasylNode
.
Parametersβ
config
: File path to a node configuration. Set toNone
to use default configuration.listener
: Callback function for node events. Must have the following signature:def on_event(event)
.
drasyl_node_identityβ
def drasyl_node_identity()
Returns the node identity.
Must be called after drasyl_node_init
.
drasyl_node_startβ
def drasyl_node_start()
Starts the node.
Must be called after drasyl_node_init
.
drasyl_node_stopβ
def drasyl_node_stop()
Stops the node.
Must be called after drasyl_node_init
.
drasyl_node_sendβ
def drasyl_node_send(recipient, payload)
Sends a message payload
to recipient
.
Parametersβ
recipient
: The recipient as 64 hex characters long drasyl address.payload
: The payload to sent.
drasyl_node_is_onlineβ
def drasyl_node_is_online()
Returns True
if the node is currently online. Otherwise, False
is returned.
Must be called after drasyl_node_init
.
drasyl_shutdown_event_loopβ
def drasyl_shutdown_event_loop()
Shutdown all (if any) threads implicitly created that are used by the drasyl node. This operation cannot be undone. After performing this operation, now new node can be started!
Exampleβ
A fully functional example can be found in our GitHub Repository.