Update Python bindings to 1.4.0 API (WIP)
This commit is contained in:
@@ -1,106 +1,92 @@
|
||||
'''Example low-level socket usage'''
|
||||
"""Example low-level socket usage"""
|
||||
|
||||
import time
|
||||
import sys
|
||||
|
||||
import libzt
|
||||
|
||||
|
||||
def print_usage():
|
||||
'''print help'''
|
||||
"""print help"""
|
||||
print(
|
||||
"\nUsage: <server|client> <id_path> <nwid> <zt_service_port> <remote_ip> <remote_port>\n"
|
||||
"\nUsage: <server|client> <storage_path> <net_id> <remote_ip> <remote_port>\n"
|
||||
)
|
||||
print("Ex: python3 demo.py server . 0123456789abcdef 9994 8080")
|
||||
print("Ex: python3 demo.py client . 0123456789abcdef 9994 192.168.22.1 8080\n")
|
||||
if len(sys.argv) < 6:
|
||||
print("Ex: python3 example.py server . 0123456789abcdef 8080")
|
||||
print("Ex: python3 example.py client . 0123456789abcdef 192.168.22.1 8080\n")
|
||||
if len(sys.argv) < 5:
|
||||
print("Too few arguments")
|
||||
if len(sys.argv) > 7:
|
||||
if len(sys.argv) > 6:
|
||||
print("Too many arguments")
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
is_joined = False # Flags to keep state
|
||||
is_online = False # Flags to keep state
|
||||
#
|
||||
# (Optional) Event handler
|
||||
#
|
||||
def on_zerotier_event(event_code, id):
|
||||
if event_code == libzt.ZTS_EVENT_NODE_ONLINE:
|
||||
print("ZTS_EVENT_NODE_ONLINE (" + str(event_code) + ") : " + hex(id))
|
||||
if event_code == libzt.ZTS_EVENT_NODE_OFFLINE:
|
||||
print("ZTS_EVENT_NODE_OFFLINE (" + str(event_code) + ") : " + hex(id))
|
||||
if event_code == libzt.ZTS_EVENT_NETWORK_READY_IP4:
|
||||
print("ZTS_EVENT_NETWORK_READY_IP4 (" + str(event_code) + ") : " + hex(id))
|
||||
if event_code == libzt.ZTS_EVENT_NETWORK_READY_IP6:
|
||||
print("ZTS_EVENT_NETWORK_READY_IP6 (" + str(event_code) + ") : " + hex(id))
|
||||
if event_code == libzt.ZTS_EVENT_PEER_DIRECT:
|
||||
print("ZTS_EVENT_PEER_DIRECT (" + str(event_code) + ") : " + hex(id))
|
||||
if event_code == libzt.ZTS_EVENT_PEER_RELAY:
|
||||
print("ZTS_EVENT_PEER_RELAY (" + str(event_code) + ") : " + hex(id))
|
||||
|
||||
#
|
||||
# Event handler
|
||||
#
|
||||
class MyEventCallbackClass(libzt.EventCallbackClass):
|
||||
def on_zerotier_event(self, msg):
|
||||
global is_online
|
||||
global is_joined
|
||||
print("eventCode=", msg.eventCode)
|
||||
if msg.eventCode == libzt.ZTS_EVENT_NODE_ONLINE:
|
||||
print("ZTS_EVENT_NODE_ONLINE")
|
||||
print("nodeId=" + hex(msg.node.address))
|
||||
# The node is now online, you can join/leave networks
|
||||
is_online = True
|
||||
if msg.eventCode == libzt.ZTS_EVENT_NODE_OFFLINE:
|
||||
print("ZTS_EVENT_NODE_OFFLINE")
|
||||
if msg.eventCode == libzt.ZTS_EVENT_NETWORK_READY_IP4:
|
||||
print("ZTS_EVENT_NETWORK_READY_IP4")
|
||||
is_joined = True
|
||||
# The node has successfully joined a network and has an address
|
||||
# you can perform network calls now
|
||||
if msg.eventCode == libzt.ZTS_EVENT_PEER_DIRECT:
|
||||
print("ZTS_EVENT_PEER_DIRECT")
|
||||
if msg.eventCode == libzt.ZTS_EVENT_PEER_RELAY:
|
||||
print("ZTS_EVENT_PEER_RELAY")
|
||||
|
||||
#
|
||||
# Main
|
||||
#
|
||||
def main():
|
||||
global is_online
|
||||
global is_joined
|
||||
mode = None # client|server
|
||||
storage_path = "." # Where identity files are stored
|
||||
net_id = 0 # Network to join
|
||||
remote_ip = None # ZeroTier IP of remote node
|
||||
remote_port = 8080 # ZeroTier port your app logic may use
|
||||
|
||||
key_file_path = "." # Where identity files are stored
|
||||
network_id = 0 # Network to join
|
||||
# Port used by ZeroTier to send encpryted UDP traffic
|
||||
# NOTE: Should be different from other instances of ZeroTier
|
||||
# running on the same machine
|
||||
zt_service_port = 9997
|
||||
remote_ip = None # ZeroTier IP of remote node
|
||||
remote_port = 8080 # ZeroTier port your app logic may use
|
||||
mode = None # client|server
|
||||
|
||||
if len(sys.argv) < 6 or len(sys.argv) > 7:
|
||||
if len(sys.argv) < 5 or len(sys.argv) > 6:
|
||||
print_usage()
|
||||
if sys.argv[1] == "server" and len(sys.argv) == 6:
|
||||
if sys.argv[1] == "server" and len(sys.argv) == 5:
|
||||
mode = sys.argv[1]
|
||||
key_file_path = sys.argv[2]
|
||||
network_id = int(sys.argv[3], 16)
|
||||
zt_service_port = int(sys.argv[4])
|
||||
storage_path = sys.argv[2]
|
||||
net_id = int(sys.argv[3], 16)
|
||||
remote_port = int(sys.argv[4])
|
||||
if sys.argv[1] == "client" and len(sys.argv) == 6:
|
||||
mode = sys.argv[1]
|
||||
storage_path = sys.argv[2]
|
||||
net_id = int(sys.argv[3], 16)
|
||||
remote_ip = sys.argv[4]
|
||||
remote_port = int(sys.argv[5])
|
||||
if sys.argv[1] == "client" and len(sys.argv) == 7:
|
||||
mode = sys.argv[1]
|
||||
key_file_path = sys.argv[2]
|
||||
network_id = int(sys.argv[3], 16)
|
||||
zt_service_port = int(sys.argv[4])
|
||||
remote_ip = sys.argv[5]
|
||||
remote_port = int(sys.argv[6])
|
||||
if mode is None:
|
||||
print_usage()
|
||||
print("mode = ", mode)
|
||||
print("path = ", key_file_path)
|
||||
print("network_id = ", network_id)
|
||||
print("zt_service_port = ", zt_service_port)
|
||||
print("remote_ip = ", remote_ip)
|
||||
print("remote_port = ", remote_port)
|
||||
print("mode = ", mode)
|
||||
print("storage_path = ", storage_path)
|
||||
print("net_id = ", net_id)
|
||||
print("remote_ip = ", remote_ip)
|
||||
print("remote_port = ", remote_port)
|
||||
|
||||
#
|
||||
# Example start and join logic
|
||||
# Node initialization and start
|
||||
#
|
||||
print("Starting ZeroTier...")
|
||||
event_callback = MyEventCallbackClass()
|
||||
libzt.start(key_file_path, event_callback, zt_service_port)
|
||||
|
||||
n = libzt.ZeroTierNode()
|
||||
n.init_set_event_handler(on_zerotier_event) # Optional
|
||||
n.init_from_storage(storage_path) # Optional
|
||||
n.init_set_port(9994) # Optional
|
||||
n.node_start()
|
||||
|
||||
print("Waiting for node to come online...")
|
||||
while not is_online:
|
||||
while not n.node_is_online():
|
||||
time.sleep(1)
|
||||
print("Joining network:", hex(net_id))
|
||||
n.net_join(net_id)
|
||||
while not n.net_transport_is_ready(net_id):
|
||||
time.sleep(1)
|
||||
print("Joining network:", hex(network_id))
|
||||
libzt.join(network_id)
|
||||
while not is_joined:
|
||||
time.sleep(1) # You can ping this app at this point
|
||||
print("Joined network")
|
||||
|
||||
#
|
||||
@@ -140,7 +126,7 @@ def main():
|
||||
try:
|
||||
print("connecting...")
|
||||
client.connect((remote_ip, remote_port))
|
||||
data = "Hello, world!"
|
||||
data = "Hello, roots!"
|
||||
print("send: ", data)
|
||||
sent_bytes = client.send(data)
|
||||
print("sent: " + str(sent_bytes) + " byte(s)")
|
||||
@@ -150,5 +136,6 @@ def main():
|
||||
print(ex)
|
||||
print("errno=", libzt.errno())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user