1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import sys from kazoo.client import KazooClient, KazooState import logging
logging.basicConfig( level=logging.DEBUG, stream=sys.stdout, format='%(asctime)s %(pathname)s %(funcName)s%(lineno)d %(levelname)s: %(message)s')
zk = KazooClient( hosts='10.167.218.158:2181', timeout=10.0, logger=logging ) # 开始心跳 zk.start()
# 获取根节点数据和状态 data, stat = zk.get('/') print("data:", data) print("stat:", stat)
# 获取子节点 children = zk.get_children("/") print(children)
# 创建子节点 # zk.create('/sanguo/shuguo', b'caoChao', ephemeral=False) if not zk.exists('/sanguo/weiguo'): zk.create('/sanguo/weiguo', b'caoChao', ephemeral=False)
children = zk.get_children("/sanguo") print(children) data, stat = zk.get('/sanguo/weiguo') print("data:", data) print("stat:", stat)
def monitor(event): print(type(event)) print(event) print(zk.get_children('/sanguo'))
import time
children = zk.get_children('/sanguo', watch=monitor) print(children)
node = zk.create('/b') print(node, 'aaaaaaaaaaaaaaaaaaaaa') print(zk.exists('/sanguo'), 'fafafafafaa')
|