Python-consul is a client library for interacting with the Consul HTTP API. It provides a comprehensive set of features for service discovery, key-value storage, health checking, and session management. The library is currently at version 1.1.0 and is actively maintained with a moderate release cadence.
pip install python-consulVerified import paths — ran on the pinned version, not inferred.
Initializes a Consul client, sets and retrieves a key-value pair, and registers a simple service. It demonstrates configuring the connection using environment variables and basic error handling.
Access data via the `data` tuple element returned by the API call, which directly contains the parsed JSON. For example, `index, data = c.kv.get('foo')` where `data` is the dictionary payload.Update call sites to expect a string return from `acl.create`. Refactor any usage of `acl.clone` to use `acl.create` with the desired parameters.
Convert integer `ttl` values to their string representations (e.g., `30` becomes `'30s'`).
Always capture the `index` value (e.g., `index, data = c.kv.get('foo')`) and pass it as the `index` argument for subsequent blocking calls (`index, new_data = c.kv.get('foo', index=index)`).Ensure `token` is passed either during `consul.Consul()` initialization or as an argument to specific API calls that require it.
pip install python-consul
Ensure the Consul agent is running and accessible at the specified host and port. If necessary, configure the client with `c = consul.Consul(host='<ip>', port=<port>)`.
To check for key existence, retrieve it and check if the returned `data` is `None`: `index, data = c.kv.get('my_key'); if data is None: print('Key not found')`.Increase the timeout when initializing the Consul client: `c = consul.Consul(timeout=10.0)`.