This repo hosts sample code to showcase how the Solace Python API could be used. You can find:
/patterns
--> runnable code showcasing different message exchange patters with the Python API/howtos
--> runnable code snippets showcasing how to use different features of the API. All howtos are namedhow_to_*.py
with some sampler files under sub folders.
- Install Python3 (See installed version using
python3 -V
)
1.1 Note: If you are installing python for the first time on your machine then you can just usepython
instead ofpython3
for the commands - [Optional] Install virtualenv
python3 -m pip install --user virtualenv
1.1 Note: on a Linux machine, depending on the distribution you might need toapt-get install python3-venv
instead 1.2 Alternatively, you can usepyenv
to manage multiple versions of python. Follow the instructions here for more information https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv - Clone this repository
- [Optional] Setup python virtual environment
python3 -m venv venv
- [Optional] Activate virtual environment:
1.1 MacOS/Linux:source venv/bin/activate
1.2 Windows:venv/Scripts/activate
- After activating the virtual environment, make sure you have the latest pip installed
pip install --upgrade pip
- Install the API
pip install -r requirements.txt
Execute the script of choice as follows:
python patterns/<name_of_file>.py
Note: This assumes you have a local docker broker running on localhost
To pass non default parameters, do so via the environment variables
SOLACE_HOST=<host_name> SOLACE_VPN=<vpn_name> SOLACE_USERNAME=<username> SOLACE_PASSWORD=<password> python <name_of_file>.py
To run any of the howtos samples, you will have to set the PYTHONPATH
environment variable to the current directory since there are references to local modules. To do so you can run any of the howtos samples as follows
PYTHONPATH=.. python <name_of_file>.py
PYTHONPATH=.. python sampler_master.py
All connection details are provided in the solbroker_properties.json
file include the SEMPv2 connection details and client connection detail with default value for a localhost deployment of the PubSub+ software broker. Update the solbroker_properties.json
file entries to point the connections to a different broker.
The fixtures folder contains the sample client certificate api-client.pem and its CA public_root_ca.crt. Note the howtoes automatically adds a client certificate authority on the remote broker via SEMPv2.
TLS downgrade requires some manual remote broker configuration as well as the message vpn on the remote broker does not enable the feature to tls downgrade to plain text by default and must be confirmed. In the future this might be automatically enabled via SEMPv2 for the duration of the sampler execution.
Note the trusted certificate for the remote broker must be added to the fixtures folder as that directory is the trustore to establish TLS connections for the how_to*.py
samplers. For details on how to add a server certifcate to the broker see Managing Server Certs.
For example should the broker have a server certificate derived from the public_root_ca.crt then TLS connection can be established since the public_root_ca.crt is in the truststore located at fixtures.
- Python Virtual environment is recommended to keep your project dependencies within the project scope and avoid polluting global python packages
- Solace hostname, username, message vpn, and password are obtained from your Solace cloud account
- Make sure you have the latest pip version installed prior installation
- Solace Developer Portal is at solace.dev
- Ask the Solace Community for further discussions and questions.
- Official python documentation on https://docs.solace.com/Solace-PubSub-Messaging-APIs/Python-API/python-home.htm