15.1.3. Connections

This module defines the ConnectionManager class which is used together with ServiceManager class to support seamlessly following links and relations from one service to another, whether on the same host or on different hosts.

A single ConnectionManager instance instantiates connections to target hosts by calling registered ConnectionHook instances. A connection is established for each unique <host, auth> pair, where the auth object represents the authentication credentials in a form understood by the underlying connection class. class Connection

class sleepwalker.connection.Connection(hostname, auth=None, port=None, verify=True, timeout=None)

Handle authentication and communication to remote machines.

__init__(hostname, auth=None, port=None, verify=True, timeout=None)

Initialize new connection and setup authentication

hostname - include protocol, e.g. ‘https://host.comauth - authentication object, see below port - optional port to use for connection verify - require SSL certificate validation. timeout - float connection timeout in seconds, or tuple

(connect timeout, read timeout)

Authentication: For simple basic auth, passing a tuple of (user, pass) is sufficient as a shortcut to an instance of HTTPBasicAuth. This auth method will trigger a check to ensure the protocol is using SSL to connect (though cert verification may still be turned off to avoid errors with self-signed certs).

netrc config files will be checked if auth is left as None. If no authentication is provided for the hostname in the netrc file, or no file exists, an error will be raised when trying to connect.

class JsonEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)

Handle more object types if first encoding doesn’t work.


Add headers that are common to all requests.


Returns a fully qualified URL given a URI.

Connection.json_request(method, uri, body=None, params=None, extra_headers=None)

Send a JSON request and receive JSON response. class ConnectionManager

class sleepwalker.connection.ConnectionManager
add(host, auth, conn)

Manually add a connection to the given host to the manager.

  • host – the target host of the connection
  • auth – object representing authentication credentials
  • conn – a Connection object

Note that if a connection is already present to the target host it is replaced with the new connection.


Add a connection hook to call to establish new connections.


Drop all connection hooks.

find(host, auth)

Find a connection to the given host, trying hooks as needed.

  • host – the target host of the connection
  • auth – object representing authentication credentials

ConnectionError – if no connection to the target host could be found and no connection hooks succeeded in establishing a new connection


Close and forget all connections. class ConnectionHook

class sleepwalker.connection.ConnectionHook

This class defines the interface for establshing connections for ConnectionManager.

This class must be sub-classed and the connect() method implemented. An instance of the new class is passed to the ConnectionManager.add_conn_hook() method.

connect(host, auth)

Establish a new Connection to the target host.

  • host – scheme / ip address or hostname / port of the target server to connect to
  • auth – object representing authentication credentials to use for requests to the target host

This method must return a Connection object (or similar) or None if this hook does not know how to connect to the named host.