python-etcd: etcd v2 Client
A Python client library for etcd. This library specifically targets and supports the etcd v2 API. The last release (0.4.5) was in March 2017, and it is no longer actively maintained. It has no stated release cadence.
Warnings
- breaking This library is strictly designed for and only supports the etcd v2 API. It will NOT work with etcd v3 or later versions, which are the current standard for etcd clusters. Attempts to connect to an etcd v3 cluster will result in connection errors or unexpected behavior.
- deprecated The library is unmaintained since 2017 (last release 0.4.5). This means it is unlikely to receive updates for new Python versions, bug fixes, or security patches. Using it with modern Python environments (e.g., Python 3.8+) may lead to compatibility issues.
- breaking Starting from version 0.4.1, explicit support for etcd-backed locking and leader election features was removed from this library. If your application relies on these specific features, using versions 0.4.1 and above will break existing code.
Install
-
pip install python-etcd
Imports
- Client
from etcd import Client
Quickstart
import etcd
import os
# Configure etcd host and port (defaults to localhost:2379 for etcd v2)
# Replace with your etcd v2 cluster address
etcd_host = os.environ.get('ETCD_HOST', '127.0.0.1')
etcd_port = int(os.environ.get('ETCD_PORT', 2379))
try:
# Initialize client for etcd v2
client = etcd.Client(host=etcd_host, port=etcd_port)
print(f"Connected to etcd v2 at {etcd_host}:{etcd_port}")
# Write a key-value pair
client.write('/mykey', 'myvalue')
print("Wrote '/mykey': 'myvalue'")
# Read the value back
result = client.read('/mykey')
print(f"Read '/mykey': {result.value}")
# Update a key with a TTL (Time To Live)
client.write('/tempkey', 'tempvalue', ttl=5)
print("Wrote '/tempkey' with a 5-second TTL")
# Delete a key
client.delete('/mykey')
print("Deleted '/mykey'")
except etcd.EtcdException as e:
print(f"An etcd error occurred: {e}")
except ConnectionRefusedError:
print(f"Connection to etcd v2 at {etcd_host}:{etcd_port} refused. Is etcd v2 running?")
except Exception as e:
print(f"An unexpected error occurred: {e}")