Python Client

Forest Bus Python Client Library

This is a Python client library for Forest Bus, compatible with both Python 2 and 3.

The library requires the cbor library, installable using pip install cbor.  The easiest way to install forestbus is with pip install forestbus, alternatively you can download the source distribution file directly below.

Documentation

The Forest Bus client library Python API documentation is included in the source distribution as well as on this site.

Download

Release 1.0.3

Compatible with Forest Bus server 1.0.

Quick Start

# Example Python code showing sending and receiving from a topic on Forest Bus.
# 
# The example requires forest-bus-server to be running with three nodes with a
# cluster Id of "testcluster" and cbor listening on ports 5000 through 5002.
# For example:
# forest-bus-server -id testcluster -cbor :5000 -name :3000 -path ~/forest-data/bus1
# forest-bus-server -id testcluster -cbor :5001 -name :3001 -path ~/forest-data/bus2
# forest-bus-server -id testcluster -cbor :5002 -name :3002 -path ~/forest-data/bus3
#
# On first start the servers need configuring with their peers:
# forest-admin -id testcluster peers :3000,:3001,:3002
#
# A test topic also needs to have been created:
# forest-admin -id testcluster topic test
#
import json
import socket
from forestbus import forest

print ("Attempting client connection.")

client = forest.Client ("testcluster", ["localhost:5000","localhost:5001", "localhost:5002"])

msgs = json.dumps ({"Test field 1": "Hello", "Test field 2": "World"})

try:
	print ("Sending messages to the test topic")
	ids = client.SendMessages("test", [msgs], True)
	print(("Resulting IDs: " + str (ids)))

	print ("Calling GetMessages on topic test")
	result = client.GetMessages ("test", 1, 1, True)
	print (result)

except Exception as e:
	print(("Unexpected error during test: " + str (e)))

print ("Requesting close")
client.Close()

Last Modified: Sun, 14 Jun 2015 17:18:49 BST

Made with PubTal 3.5

Copyright 2017 Colin Stewart

Email: colin at owlfish.com