python-etcd documentation
=========================
A python client for Etcd https://github.com/coreos/etcd
Official documentation: http://python-etcd.readthedocs.org/
.. image:: https://travis-ci.org/jplana/python-etcd.png?branch=master
:target: https://travis-ci.org/jplana/python-etcd
.. image:: https://coveralls.io/repos/jplana/python-etcd/badge.svg?branch=master&service=github
:target: https://coveralls.io/github/jplana/python-etcd?branch=master
Installation
------------
Pre-requirements
~~~~~~~~~~~~~~~~
This version of python-etcd will only work correctly with the etcd server version 2.0.x or later. If you are running an older version of etcd, please use python-etcd 0.3.3 or earlier.
This client is known to work with python 2.7 and with python 3.3 or above. It is not tested or expected to work in more outdated versions of python.
From source
~~~~~~~~~~~
.. code:: bash
$ python setup.py install
Usage
-----
The basic methods of the client have changed compared to previous versions, to reflect the new API structure; however a compatibility layer has been maintained so that you don't necessarily need to rewrite all your existing code.
Create a client object
~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
import etcd
client = etcd.Client() # this will create a client against etcd server running on localhost on port 4001
client = etcd.Client(port=4002)
client = etcd.Client(host='127.0.0.1', port=4003)
client = etcd.Client(host=(('127.0.0.1', 4001), ('127.0.0.1', 4002), ('127.0.0.1', 4003)))
client = etcd.Client(host='127.0.0.1', port=4003, allow_redirect=False) # wont let you run sensitive commands on non-leader machines, default is true
# If you have defined a SRV record for _etcd._tcp.example.com pointing to the clients
client = etcd.Client(srv_domain='example.com', protocol="https")
# create a client against https://api.example.com:443/etcd
client = etcd.Client(host='api.example.com', protocol='https', port=443, version_prefix='/etcd')
Write a key
~~~~~~~~~~~
.. code:: python
client.write('/nodes/n1', 1)
# with ttl
client.write('/nodes/n2', 2, ttl=4) # sets the ttl to 4 seconds
client.set('/nodes/n2', 1) # Equivalent, for compatibility reasons.
Read a key
~~~~~~~~~~
.. code:: python
client.read('/nodes/n2').value
client.read('/nodes', recursive = True) #get all the values of a directory, recursively.
client.get('/nodes/n2').value
# raises etcd.EtcdKeyNotFound when key not found
try:
client.read('/invalid/path')
except etcd.EtcdKeyNotFound:
# do something
print "error"
Delete a key
~~~~~~~~~~~~
.. code:: python
client.delete('/nodes/n1')
Atomic Compare and Swap
~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
client.write('/nodes/n2', 2, prevValue = 4) # will set /nodes/n2 's value to 2 only if its previous value was 4 and
client.write('/nodes/n2', 2, prevExist = False) # will set /nodes/n2 's value to 2 only if the key did not exist before
client.write('/nodes/n2', 2, prevIndex = 30) # will set /nodes/n2 's value to 2 only if the key was last modified at index 30
client.test_and_set('/nodes/n2', 2, 4) #equivalent to client.write('/nodes/n2', 2, prevValue = 4)
You can also atomically update a result:
.. code:: python
result = client.read('/foo')
print(result.value) # bar
result.value += u'bar'
updated = client.update(result) # if any other client wrote '/foo' in the meantime this will fail
print(updated.value) # barbar
Watch a key
~~~~~~~~~~~
.. code:: python
client.read('/nodes/n1', wait = True) # will wait till the key is changed, and return once its changed
client.read('/nodes/n1', wait = True, timeout=30) # will wait till the key is changed, and return once its changed, or exit with an exception after 30 seconds.
client.read('/nodes/n1', wait = True, waitIndex = 10) # get all changes on this key starting from index 10
client.watch('/nodes/n1') #equivalent to client.read('/nodes/n1', wait = True)
client.watch('/nodes/n1', index = 10)
Refreshing key TTL
~~~~~~~~~~~~~~~~~~
(Since etcd 2.3.0) Keys in etcd can be refreshed without notifying current watchers.
This can be achieved by setting the refresh to true when updating a TTL.
You cannot update the value of a key when refreshing it.
.. code:: python
client.write('/nodes/n1', 'value', ttl=30) # sets the ttl to 30 seconds
client.refresh('/nodes/n1', ttl=600) # refresh ttl to 600 seconds, without notifying current watchers
Locking module
~~~~~~~~~~~~~~
.. code:: python
# Initialize the lock object:
# NOTE: this does not acquire a lock yet
client = etcd.Client()
# Or you can custom lock prefix, default is '/_locks/' if you are using HEAD
client = etcd.Client(lock_prefix='/my_etcd_root/_locks')
lock = etcd.Lock(client, 'my_lock_name')
# Use the lock object:
lock.acquire(blocking=True, # will block until the lock is acquired
lock_ttl=None) # lock will live until we release it
lock.is_acquired # True
lock.acquire(lock_ttl=60) # renew a lock
lock.release() # release an existing lock
lock.is_acquired # False
# The lock object may also be used as a context manager:
client = etcd.Client()
with etcd.Lock(client, 'customer1') as my_lock:
do_stuff()
my_lock.is_acquired # True
my_lock.acquire(lock_ttl=60)
my_lock.is_acquired # False
Get machines in the cluster
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
client.machines
Get leader of the cluster
~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
client.leader
Generate a sequential key in a directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
x = client.write("/dir/name", "value", append=True)
print("generated key: " + x.key)
print("stored value: " + x.value)
List contents of a directory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: python
#stick a couple values in the directory
client.write("/dir/name", "value1", append=True)
client.write("/dir/name", "value2", append=True)
directory = client.get("/dir/name")
# loop through directory children
for result in directory.children:
print(result.key + ": " + result.value)
# or just get the first child value
print(directory.children.next().value)
Development setup
-----------------
To create a buildout,
.. code:: bash
$ python bootstrap.py
$ bin/buildout
to test you should have etcd available in your system path:
.. code:: bash
$ bin/test
to generate documentation,
.. code:: bash
$ cd docs
$ make
Release HOWTO
-------------
To make a release
1) Update release date/version in NEWS.txt and setup.py
2) Run 'python setup.py sdist'
3) Test the generated source distribution in dist/
4) Upload to PyPI: 'python setup.py sdist register upload'
News
====
0.4.5
-----
*Release date: 3-Mar-2017*
* Remove dnspython2/3 requirement
* Change property name setter in lock
* Fixed acl tests
* Added version/cluster_version properties to client
* Fixes in lock when used as context manager
* Fixed improper usage of urllib3 exceptions
* Minor fixes for error classes
* In lock return modifiedIndex to watch changes
* In lock fix context manager exception handling
* Improvments to the documentation
* Remove _base_uri only after refresh from cluster
* Avoid double update of _machines_cache
0.4.4
-----
*Release date: 10-Jan-2017*
* Fix some tests
* Use sys,version_info tuple, instead of named tuple
* Improve & fix documentation
* Fix python3 specific problem when blocking on contented lock
* Add refresh key method
* Add custom lock prefix support
0.4.3
-----
*Release date: 14-Dec-2015*
* Fix check for parameters in case of connection error
* Python 3.5 compatibility and general python3 cleanups
* Added authentication and module for managing ACLs
* Added srv record-based DNS discovery
* Fixed (again) logging of cluster id changes
* Fixed leader lookup
* Properly retry request on exception
* Client: clean up open connections when deleting
0.4.2
-----
*Release date: 8-Oct-2015*
* Fixed lock documentation
* Fixed lock sequences due to etcd 2.2 change
* Better exception management during response processing
* Fixed logging of cluster ID changes
* Fixed subtree results
* Do not check cluster ID if etcd responses don't contain the ID
* Added a cause to EtcdConnectionFailed
0.4.1
-----
*Release date: 1-Aug-2015*
* Added client-side leader election
* Added stats endpoints
* Added logging
* Better exception handling
* Check for cluster ID on each request
* Added etcd.Client.members and fixed etcd.Client.leader
* Removed locking and election etcd support
* Allow the use of etcd proxies with reconnections
* Implement pop: Remove key from etc and return the corresponding value.
* Eternal watcher can be now recursive
* Fix etcd.Client machines
* Do not send parameters with `None` value to etcd
* Support ttl=0 in write.
* Moved pyOpenSSL into test requirements.
* Always set certificate information so redirects from http to https work.
0.3.3
-----
*Release date: 12-Apr-2015*
* Forward leaves_only value in get_subtree() recursive calls
* Fix README prevExists->prevExist
* Added configurable version_prefix
* Added support for recursive watch
* Better error handling support (more detailed exceptions)
* Fixed some unreliable tests
0.3.2
-----
*Release date: 4-Aug-2014*
* Fixed generated documentation version.
0.3.1
-----
*Release date: 4-Aug-2014*
* Added consisten read option
* Fixed timeout parameter in read()
* Added atomic delete parameter support
* Fixed delete behaviour
* Added update method that allows atomic updated on results
* Fixed checks on write()
* Added leaves generator to EtcdResult and get_subtree for recursive fetch
* Added etcd_index to EtcdResult
* Changed ethernal -> eternal
* Updated urllib3 & pyOpenSSL libraries
* Several performance fixes
* Better parsing of etcd_index and raft_index
* Removed duplicated tests
* Added several integration and unit tests
* Use etcd v0.3.0 in travis
* Execute test using `python setup.py test` and nose
0.3.0
-----
*Release date: 18-Jan-2014*
* API v2 support
* Python 3.3 compatibility
0.2.1
-----
*Release data: 30-Nov-2013*
* SSL support
* Added support for subdirectories in results.
* Improve test
* Added support for reconnections, allowing death node tolerance.
0.2.0
-----
*Release date: 30-Sep-2013*
* Allow fetching of multiple keys (sub-nodes)
0.1
---
*Release date: 18-Sep-2013*
* Initial release
Raw data
{
"_id": null,
"home_page": "http://github.com/jplana/python-etcd",
"name": "python-etcd",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "etcd raft distributed log api client",
"author": "Jose Plana",
"author_email": "jplana@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/a1/da/616a4d073642da5dd432e5289b7c1cb0963cc5dde23d1ecb8d726821ab41/python-etcd-0.4.5.tar.gz",
"platform": "",
"description": "python-etcd documentation\n=========================\n\nA python client for Etcd https://github.com/coreos/etcd\n\nOfficial documentation: http://python-etcd.readthedocs.org/\n\n.. image:: https://travis-ci.org/jplana/python-etcd.png?branch=master\n :target: https://travis-ci.org/jplana/python-etcd\n\n.. image:: https://coveralls.io/repos/jplana/python-etcd/badge.svg?branch=master&service=github\n :target: https://coveralls.io/github/jplana/python-etcd?branch=master\n\nInstallation\n------------\n\nPre-requirements\n~~~~~~~~~~~~~~~~\n\nThis version of python-etcd will only work correctly with the etcd server version 2.0.x or later. If you are running an older version of etcd, please use python-etcd 0.3.3 or earlier.\n\nThis client is known to work with python 2.7 and with python 3.3 or above. It is not tested or expected to work in more outdated versions of python.\n\nFrom source\n~~~~~~~~~~~\n\n.. code:: bash\n\n $ python setup.py install\n\nUsage\n-----\n\nThe basic methods of the client have changed compared to previous versions, to reflect the new API structure; however a compatibility layer has been maintained so that you don't necessarily need to rewrite all your existing code.\n\nCreate a client object\n~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n import etcd\n\n client = etcd.Client() # this will create a client against etcd server running on localhost on port 4001\n client = etcd.Client(port=4002)\n client = etcd.Client(host='127.0.0.1', port=4003)\n client = etcd.Client(host=(('127.0.0.1', 4001), ('127.0.0.1', 4002), ('127.0.0.1', 4003)))\n client = etcd.Client(host='127.0.0.1', port=4003, allow_redirect=False) # wont let you run sensitive commands on non-leader machines, default is true\n # If you have defined a SRV record for _etcd._tcp.example.com pointing to the clients\n client = etcd.Client(srv_domain='example.com', protocol=\"https\")\n # create a client against https://api.example.com:443/etcd\n client = etcd.Client(host='api.example.com', protocol='https', port=443, version_prefix='/etcd')\n\nWrite a key\n~~~~~~~~~~~\n\n.. code:: python\n\n client.write('/nodes/n1', 1)\n # with ttl\n client.write('/nodes/n2', 2, ttl=4) # sets the ttl to 4 seconds\n client.set('/nodes/n2', 1) # Equivalent, for compatibility reasons.\n\nRead a key\n~~~~~~~~~~\n\n.. code:: python\n\n client.read('/nodes/n2').value\n client.read('/nodes', recursive = True) #get all the values of a directory, recursively.\n client.get('/nodes/n2').value\n\n # raises etcd.EtcdKeyNotFound when key not found\n try:\n client.read('/invalid/path')\n except etcd.EtcdKeyNotFound:\n # do something\n print \"error\"\n\n\nDelete a key\n~~~~~~~~~~~~\n\n.. code:: python\n\n client.delete('/nodes/n1')\n\nAtomic Compare and Swap\n~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n client.write('/nodes/n2', 2, prevValue = 4) # will set /nodes/n2 's value to 2 only if its previous value was 4 and\n client.write('/nodes/n2', 2, prevExist = False) # will set /nodes/n2 's value to 2 only if the key did not exist before\n client.write('/nodes/n2', 2, prevIndex = 30) # will set /nodes/n2 's value to 2 only if the key was last modified at index 30\n client.test_and_set('/nodes/n2', 2, 4) #equivalent to client.write('/nodes/n2', 2, prevValue = 4)\n\nYou can also atomically update a result:\n\n.. code:: python\n\n result = client.read('/foo')\n print(result.value) # bar\n result.value += u'bar'\n updated = client.update(result) # if any other client wrote '/foo' in the meantime this will fail\n print(updated.value) # barbar\n\nWatch a key\n~~~~~~~~~~~\n\n.. code:: python\n\n client.read('/nodes/n1', wait = True) # will wait till the key is changed, and return once its changed\n client.read('/nodes/n1', wait = True, timeout=30) # will wait till the key is changed, and return once its changed, or exit with an exception after 30 seconds.\n client.read('/nodes/n1', wait = True, waitIndex = 10) # get all changes on this key starting from index 10\n client.watch('/nodes/n1') #equivalent to client.read('/nodes/n1', wait = True)\n client.watch('/nodes/n1', index = 10)\n\nRefreshing key TTL\n~~~~~~~~~~~~~~~~~~\n\n(Since etcd 2.3.0) Keys in etcd can be refreshed without notifying current watchers.\n\nThis can be achieved by setting the refresh to true when updating a TTL.\n\nYou cannot update the value of a key when refreshing it.\n\n.. code:: python\n\n client.write('/nodes/n1', 'value', ttl=30) # sets the ttl to 30 seconds\n client.refresh('/nodes/n1', ttl=600) # refresh ttl to 600 seconds, without notifying current watchers\n\nLocking module\n~~~~~~~~~~~~~~\n\n.. code:: python\n\n # Initialize the lock object:\n # NOTE: this does not acquire a lock yet\n client = etcd.Client()\n # Or you can custom lock prefix, default is '/_locks/' if you are using HEAD\n client = etcd.Client(lock_prefix='/my_etcd_root/_locks')\n lock = etcd.Lock(client, 'my_lock_name')\n\n # Use the lock object:\n lock.acquire(blocking=True, # will block until the lock is acquired\n lock_ttl=None) # lock will live until we release it\n lock.is_acquired # True\n lock.acquire(lock_ttl=60) # renew a lock\n lock.release() # release an existing lock\n lock.is_acquired # False\n\n # The lock object may also be used as a context manager:\n client = etcd.Client()\n with etcd.Lock(client, 'customer1') as my_lock:\n do_stuff()\n my_lock.is_acquired # True\n my_lock.acquire(lock_ttl=60)\n my_lock.is_acquired # False\n\n\nGet machines in the cluster\n~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n client.machines\n\nGet leader of the cluster\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n client.leader\n\nGenerate a sequential key in a directory\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n x = client.write(\"/dir/name\", \"value\", append=True)\n print(\"generated key: \" + x.key)\n print(\"stored value: \" + x.value)\n\nList contents of a directory\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: python\n\n #stick a couple values in the directory\n client.write(\"/dir/name\", \"value1\", append=True)\n client.write(\"/dir/name\", \"value2\", append=True)\n\n directory = client.get(\"/dir/name\")\n\n # loop through directory children\n for result in directory.children:\n print(result.key + \": \" + result.value)\n\n # or just get the first child value\n print(directory.children.next().value)\n\nDevelopment setup\n-----------------\n\nTo create a buildout,\n\n.. code:: bash\n\n $ python bootstrap.py\n $ bin/buildout\n\nto test you should have etcd available in your system path:\n\n.. code:: bash\n\n $ bin/test\n\nto generate documentation,\n\n.. code:: bash\n\n $ cd docs\n $ make\n\nRelease HOWTO\n-------------\n\nTo make a release\n\n 1) Update release date/version in NEWS.txt and setup.py\n 2) Run 'python setup.py sdist'\n 3) Test the generated source distribution in dist/\n 4) Upload to PyPI: 'python setup.py sdist register upload'\n\n\nNews\n====\n0.4.5\n-----\n*Release date: 3-Mar-2017*\n\n* Remove dnspython2/3 requirement\n* Change property name setter in lock\n* Fixed acl tests\n* Added version/cluster_version properties to client\n* Fixes in lock when used as context manager\n* Fixed improper usage of urllib3 exceptions\n* Minor fixes for error classes\n* In lock return modifiedIndex to watch changes\n* In lock fix context manager exception handling\n* Improvments to the documentation\n* Remove _base_uri only after refresh from cluster\n* Avoid double update of _machines_cache\n\n\n0.4.4\n-----\n*Release date: 10-Jan-2017*\n\n* Fix some tests\n* Use sys,version_info tuple, instead of named tuple\n* Improve & fix documentation\n* Fix python3 specific problem when blocking on contented lock\n* Add refresh key method\n* Add custom lock prefix support\n\n\n0.4.3\n-----\n*Release date: 14-Dec-2015*\n\n* Fix check for parameters in case of connection error\n* Python 3.5 compatibility and general python3 cleanups\n* Added authentication and module for managing ACLs\n* Added srv record-based DNS discovery\n* Fixed (again) logging of cluster id changes\n* Fixed leader lookup\n* Properly retry request on exception\n* Client: clean up open connections when deleting\n\n0.4.2\n-----\n*Release date: 8-Oct-2015*\n\n* Fixed lock documentation\n* Fixed lock sequences due to etcd 2.2 change\n* Better exception management during response processing\n* Fixed logging of cluster ID changes\n* Fixed subtree results\n* Do not check cluster ID if etcd responses don't contain the ID\n* Added a cause to EtcdConnectionFailed\n\n\n0.4.1\n-----\n*Release date: 1-Aug-2015*\n\n* Added client-side leader election\n* Added stats endpoints\n* Added logging\n* Better exception handling\n* Check for cluster ID on each request\n* Added etcd.Client.members and fixed etcd.Client.leader\n* Removed locking and election etcd support\n* Allow the use of etcd proxies with reconnections\n* Implement pop: Remove key from etc and return the corresponding value.\n* Eternal watcher can be now recursive\n* Fix etcd.Client machines\n* Do not send parameters with `None` value to etcd\n* Support ttl=0 in write.\n* Moved pyOpenSSL into test requirements.\n* Always set certificate information so redirects from http to https work.\n\n\n0.3.3\n-----\n*Release date: 12-Apr-2015*\n\n* Forward leaves_only value in get_subtree() recursive calls\n* Fix README prevExists->prevExist\n* Added configurable version_prefix\n* Added support for recursive watch\n* Better error handling support (more detailed exceptions)\n* Fixed some unreliable tests\n\n\n0.3.2\n-----\n\n*Release date: 4-Aug-2014*\n\n* Fixed generated documentation version.\n\n\n0.3.1\n-----\n\n*Release date: 4-Aug-2014*\n\n* Added consisten read option\n* Fixed timeout parameter in read()\n* Added atomic delete parameter support\n* Fixed delete behaviour\n* Added update method that allows atomic updated on results\n* Fixed checks on write()\n* Added leaves generator to EtcdResult and get_subtree for recursive fetch\n* Added etcd_index to EtcdResult\n* Changed ethernal -> eternal\n* Updated urllib3 & pyOpenSSL libraries\n* Several performance fixes\n* Better parsing of etcd_index and raft_index\n* Removed duplicated tests\n* Added several integration and unit tests\n* Use etcd v0.3.0 in travis\n* Execute test using `python setup.py test` and nose\n\n\n0.3.0\n-----\n\n*Release date: 18-Jan-2014*\n\n* API v2 support\n* Python 3.3 compatibility\n\n\n0.2.1\n-----\n\n*Release data: 30-Nov-2013*\n\n* SSL support\n* Added support for subdirectories in results.\n* Improve test\n* Added support for reconnections, allowing death node tolerance.\n\n\n0.2.0\n-----\n\n*Release date: 30-Sep-2013*\n\n* Allow fetching of multiple keys (sub-nodes)\n\n\n0.1\n---\n\n*Release date: 18-Sep-2013*\n\n* Initial release\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A python client for etcd",
"version": "0.4.5",
"split_keywords": [
"etcd",
"raft",
"distributed",
"log",
"api",
"client"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "a636872dcaf5595ec6f71c83322b9924",
"sha256": "f1b5ebb825a3e8190494f5ce1509fde9069f2754838ed90402a8c11e1f52b8cb"
},
"downloads": -1,
"filename": "python-etcd-0.4.5.tar.gz",
"has_sig": false,
"md5_digest": "a636872dcaf5595ec6f71c83322b9924",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 37270,
"upload_time": "2017-03-02T23:05:36",
"upload_time_iso_8601": "2017-03-02T23:05:36.016866Z",
"url": "https://files.pythonhosted.org/packages/a1/da/616a4d073642da5dd432e5289b7c1cb0963cc5dde23d1ecb8d726821ab41/python-etcd-0.4.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2017-03-02 23:05:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "jplana",
"github_project": "python-etcd",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "python-etcd"
}