Package: etcd3-py
etcd3-pyPython client for etcd v3 (Using gRPC-JSON-Gateway)Free software: Apache Software License 2.0Source Code:https://github.com/Revolution1/etcd3-pyDocumentation:https://etcd3-py.readthedocs.io.etcd version required: v3.2.2+Notice: The authentication header through gRPC-JSON-Gateway only supported inetcd v3.3.0+FeaturesSupport python2.7 and python3.5+ (aiohttp requires python3.5.2+)Sync client based on requestsAsync client based on aiohttpTLS Connectionsupport APIsAuthKVWatchClusterLeaseLockMaintenanceExtra APIsstateful utilitiesWatchLeaseTransactionLockQuick StartInstall$pipinstalletcd3-pySync Client>>>frometcd3importClient>>>client=Client('127.0.0.1',2379,cert=(CERT_PATH,KEY_PATH),verify=CA_PATH)>>>client.version()EtcdVersion(etcdserver='3.3.0-rc.4',etcdcluster='3.3.0')>>>client.put('foo','bar')etcdserverpbPutResponse(header=etcdserverpbResponseHeader(cluster_id=11588568905070377092,member_id=128088275939295631,revision=15433,raft_term=4))>>>client.range('foo').kvs[mvccpbKeyValue(key=b'foo',create_revision=15429,mod_revision=15433,version=5,value=b'bar')]Async Client (Python3.5+)>>>importasyncio>>>frometcd3importAioClient>>>client=AioClient('127.0.0.1',2379)>>>asyncdefgetFoo():...awaitclient.put('foo','bar')...r=awaitclient.range('foo')...print('key:',r.kvs[0].key,'value:',r.kvs[0].value)>>>loop=asyncio.get_event_loop()>>>loop.run_until_complete(getFoo())key:b'foo'value:b'bar'Transaction Util>>>frometcd3importClient>>>txn=Client().Txn()>>>txn.compare(txn.key('foo').value=='bar')>>>txn.success(txn.put('foo','bra'))>>>txn.commit()etcdserverpbTxnResponse(header=etcdserverpbResponseHeader(cluster_id=11588568905070377092,member_id=128088275939295631,revision=15656,raft_term=4),succeeded=True,responses=[etcdserverpbResponseOp(response_put=etcdserverpbPutResponse(header=etcdserverpbResponseHeader(revision=15656)))])Lease Util>>>frometcd3importClient>>>client=Client()>>>withclient.Lease(ttl=5)aslease:...client.put('foo','bar',lease=lease.ID)...client.put('fizz','buzz',lease=lease.ID)...r=lease.time_to_live(keys=True)...assertset(r.keys)=={b'foo',b'fizz'}...assertlease.alive()Watch Util>>>frometcd3importClient>>>client=Client()>>>watcher=c.Watcher(all=True,progress_notify=True,prev_kv=True)>>>w.onEvent('f.*',lambdae:print(e.key,e.value))>>>w.runDaemon()>>># etcdctl put foo bar>>># etcdctl put foz barb'foo'b'bar'b'foz'b'bar'>>>w.stop()Lock Util>>>importtime>>>fromthreadingimportThread>>>frometcd3importClient>>>client=Client()>>>name='lock_name'>>>defuser1():...withclient.Lock(name,lock_ttl=5):...print('user1 got the lock')...time.sleep(5)...print('user1 releasing the lock')>>>defuser2():...withclient.Lock(name,lock_ttl=5):...print('user2 got the lock')...time.sleep(5)...print('user2 releasing the lock')>>>t1=Thread(target=user1,daemon=True)>>>t2=Thread(target=user2,daemon=True)>>>t1.start()>>>t2.start()>>>t1.join()>>>t2.join()user1gotthelockuser1releasingthelockuser2gotthelockuser2releasingthelockStart a single-node etcd using dockerexportNODE1=0.0.0.0exportETCD_VER=v3.3
dockerrun-d\-p2379:2379\-p2380:2380\--volume=/tmp/etcd3-data:/etcd-data\--nameetcd3quay.io/coreos/etcd:$ETCD_VER\/usr/local/bin/etcd\--data-dir=/etcd-data--namenode1\--initial-advertise-peer-urlshttp://${NODE1}:2380--listen-peer-urlshttp://${NODE1}:2380\--advertise-client-urlshttp://${NODE1}:2379--listen-client-urlshttp://${NODE1}:2379\--initial-clusternode1=http://${NODE1}:2380FAQQ:authentication seems not working? Try calling api of a auth-enabled etcd server returned error "ErrUserEmpty error:'etcdserver: user name is empty'"A:Take a look at#41, currently etcd3-py dose not authenticate automatically, you need to call client.auth() by yourself.TODOhuman friendly middle level apisable to expose json or raw response to useradd election apibenchmarkpython-etcd(etcd v2) compatible clientetcd browsersupport etcd v3.4.xHistory0.1.6 (2019-05-9)merge pull request#90Fix lease util keeping problemsmerge pull request#89Add range end and lease to txnmerge pull request#87Add handel null value as gogoproto does while modelizing response datamerge pull request#82Fix watch util issue #18 and #78merge pull request#79Improve etcd comapabitity of multiple versionsmerge pull request#51Add a base EtcdModel to all dynamic created modelmerge pull request#42Improve etcd comapabitity of multiple versions0.1.5 (2018-07-4)merge pull request#34enum34 only where it's needed0.1.4 (2018-03-30)better code qualitysupport etcd v3.2.2+0.1.3 (2018-03-21)finished lock util0.1.2 (2018-03-20)Add more testAdd watcher, transaction and lease utilYou can try it at dev environment0.1.0 (2018-03-19)Implemented all APIs of etcd3's gRPC-JSON-GatewayStateful utils (Watcher Lease Lock Transaction) are in progress