Etcd client over HTTP allows you to interact with an etcd cluster using HTTP requests.
First, install the package via pip:
pip install httpetcd
import time
from httpetcd import clients
client = clients.get_wrapped_client(
endpoints=["http://localhost:2379/"],
namespace='ns1',
timeout=100,
)
# KV locks
lock = client.kvlock.acquire(key_name="my-lock", ttl=10)
lock.refresh()
print(f"Lock ttl: {lock.ttl()}")
print(f"Lock is alive: {lock.alive()}")
lock.release()
print(f"Lock is alive (after release): {lock.alive()}")
client.kvlock.acquire(key_name="another/lock", ttl=10)
lock = list(client.kvlock.list())[0]
lock.release()
# Lease & KV
lease = client.lease.grant(ttl=5)
client.kv.new(key_name="expiring-key", value="expiring-value", lease=lease)
print(f"KV items: {list(client.kv.items())}")
client.kv.new(key_name="my-key", value="my-value")
print(f"KV items: {list(client.kv.items())}")
time.sleep(6)
print(f"KV items (after expire): {list(client.kv.items())}")