Python Client Setup
This page provides instructions for setting up the Python client, including installation and configuration steps to enable communication with the XTABLES server.
XTablesClient Python Package
Installation
To install the XTablesClient
Python package, use the following pip command:
pip install XTablesClient
Importing
After installation, you can import the XTablesClient
as follows:
from JXTABLES import XTablesClient
XTablesClient Class
The XTablesClient
class allows you to communicate with the XTABLES server using ZeroMQ (ZMQ). Below are the details of the methods and usage.
Constructor
def __init__(self, ip=None, push_port=1735, req_port=1736, sub_port=1737, buffer_size=500):
ip: The IP address of the XTABLES server. When
None
, it will resolve the host automatically.push_port: The port for the PUSH socket.
req_port: The port for the REQ socket.
sub_port: The port for the SUB socket.
buffer_size: The buffer size for the subscribe circular buffer.
The constructor connects to the XTABLES server and initializes the sockets. If no IP address is provided, the client will attempt to resolve the XTABLES hostname.
Method: shutdown()
shutdown()
def shutdown(self):
Closes all sockets and stops the subscribe handler.
Method: connect()
connect()
def connect(self):
Attempts to reconnect the sockets to the server and re-initialize subscriptions.
Method: send_push_message()
send_push_message()
def send_push_message(self, command, key, value, msg_type):
Sends a push message to the server.
command: The command to execute (e.g.,
PUT
,PUBLISH
).key: The key associated with the message.
value: The value associated with the key.
msg_type: The message type (e.g.,
BYTES
,STRING
,INT64
).
Method: publish()
publish()
def publish(self, key, value):
Publish a message to the server with the given key and value.
Method: subscribe()
subscribe()
def subscribe(self, key: str, consumer: Callable):
Subscribes to a specific key and associates a consumer function to process updates.
key: The key to subscribe to.
consumer: The consumer function to process updates.
Method: subscribe_all()
subscribe_all()
def subscribe_all(self, consumer: Callable):
Subscribes to updates for all keys and associates a consumer function to process updates.
Method: unsubscribe()
unsubscribe()
def unsubscribe(self, key: str, consumer: Callable):
Unsubscribes a specific consumer from a given key. If no consumers remain, the key is unsubscribed.
key: The key to unsubscribe from.
consumer: The consumer function to remove.
Method: unsubscribe_all()
unsubscribe_all()
def unsubscribe_all(self, consumer: Callable):
Unsubscribes a specific consumer from all keys.
PUT Methods
These methods allow you to send data to the server with different types.
putBytes: Sends a byte array.
putUnknownBytes: Sends unknown bytes.
putString: Sends a string.
putInteger: Sends an integer.
putLong: Sends a long integer.
putDouble: Sends a double.
putBoolean: Sends a boolean value.
putArray: Sends an array of values.
def putBytes(self, key, value)
def putUnknownBytes(self, key, value)
def putString(self, key, value)
def putInteger(self, key, value)
def putLong(self, key, value)
def putDouble(self, key, value)
def putBoolean(self, key, value)
def putArray(self, key, values)
GET Methods
These methods retrieve data from the server. They return None
if no data is available.
getString: Retrieves a string value.
getInteger: Retrieves an integer value.
getBoolean: Retrieves a boolean value.
getLong: Retrieves a long integer value.
getDouble: Retrieves a double value.
getArray: Retrieves an array of values.
getBytes: Retrieves a byte array.
getUnknownBytes: Retrieves unknown bytes.
def getString(self, key)
def getInteger(self, key)
def getBoolean(self, key)
def getLong(self, key)
def getDouble(self, key)
def getArray(self, key)
def getBytes(self, key)
def getUnknownBytes(self, key)
Method: ping()
ping()
def ping(self):
Sends a ping to the server and measures the round-trip time. Returns a PingResponse
object with the result.
Version and Properties Methods
get_version: Retrieves the client version.
add_client_version_property: Adds a custom property to the client version.
def get_version(self)
def add_client_version_property(self, value)
Example Usage
from JXTABLES import XTablesClient
# Initialize the client
client = XTablesClient()
# Connect to the server
client.connect()
# Publish a string message
client.publish("my_key", "Hello, XTables!")
# Subscribe to a key
def my_consumer(message):
print("Received update:", message)
client.subscribe("my_key", my_consumer)
# Retrieve a string value
value = client.getString("my_key")
print(value)
# Ping the server
ping_response = client.ping()
print(ping_response.success, ping_response.round_trip_time)
# Shut down the client
client.shutdown()
Conclusion
This documentation covers the basic usage and setup of the XTablesClient
Python package. For advanced use cases, you can leverage the methods to manage data communication with the XTABLES server.
Last updated