Consul

The consulate.Consul class is core interface for interacting with all parts of the Consul API.

Usage Examples

Here is an example where the initial consulate.Consul is created, connecting to Consul at localhost on port 8500. Once connected, a list of all service checks is returned.

import consulate

# Create a new instance of a consulate session
session = consulate.Consul()

# Get all of the service checks for the local agent
checks = session.agent.checks()

This next example creates a new Consul passing in an authorization token and then sets a key in the Consul KV service:

import consulate

session = consulate.Consul(token='5d24c96b4f6a4aefb99602ce9b60d16b')

# Set the key named release_flag to True
session.kv['release_flag'] = True

API

class consulate.Consul(host='localhost', port=8500, datacenter=None, token=None, scheme='http', adapter=None, verify=True, cert=None)

Access the Consul HTTP API via Python.

The default values connect to Consul via localhost:8500 via http. If you want to connect to Consul via a local UNIX socket, you’ll need to override both the scheme, port and the adapter like so:

consul = consulate.Consul('/path/to/socket', None, scheme='http+unix',
                          adapter=consulate.adapters.UnixSocketRequest)
services = consul.agent.services()
Parameters:
  • host (str) – The host name to connect to (Default: localhost)
  • port (int) – The port to connect on (Default: 8500)
  • datacenter (str) – Specify a specific data center
  • token (str) – Specify a ACL token to use
  • scheme (str) – Specify the scheme (Default: http)
  • adapter (class) – Specify to override the request adapter (Default: consulate.adapters.Request)
  • verify (bool/str) – Specify how to verify TLS certificates
  • cert (tuple) – Specify client TLS certificate and key files
acl

Access the Consul ACL API

Return type:consulate.api.acl.ACL
agent

Access the Consul Agent API

Return type:consulate.api.agent.Agent
catalog

Access the Consul Catalog API

Return type:consulate.api.catalog.Catalog
event

Access the Consul Events API

Return type:consulate.api.event.Event
health

Access the Consul Health API

Return type:consulate.api.health.Health
kv

Access the Consul KV API

Return type:consulate.api.kv.KV
lock

Wrapper for easy KV locks.

Example:

import consulate

consul = consulate.Consul()
with consul.lock.acquire('my-key'):
    print('Locked: {}'.format(consul.lock.key))
    # Do stuff
Return type:Lock
session

Access the Consul Session API

Return type:consulate.api.session.Session
status

Access the Consul Status API

Return type:consulate.api.status.Status