🎄 Happy Holidays! 🥳

Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.

Happy Holidays!

Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.

Python program connecting over TLS/SSL to PubSub+ Cloud

ludobar
ludobar Member Posts: 10

Hello everyone,
I want to share a simple Python program that connects to the cloud broker service over TLS and publishes a message.
It took some time for me to understand some concepts and configuration settings, so I hope this can help anyone struggling with TLS connection.

import paho.mqtt.client as mqtt
from time import sleep

"""
The callback for when the client receives a CONNACK response from the server.
"""
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))

def on_publish(client, userdata, mid):
print(client, userdata, mid)

def on_log(mqttc, obj, level, string):
print(string)

"""
creating a client instance
"""
client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp")

"""
the properties of this method are critical for the connection to work. Incorrect properties create issues
I kept only the essential one.
the ca_certs file.pem downloaded from service -> connect -> Solace Messaging
"""
client.tls_set(ca_certs="path_to_pem_file")

"""
without server certification
please note that the method " client.tls_insecure_set(True) " checks only the hostname:
the server certificate is still verified. To disable the verification of the server
certificate use:

client.tls_set(cert_reqs=ssl.CERT_NONE)
"""

client.on_publish = on_publish
client.on_connect = on_connect
client.on_log = on_log

"""
setting password and username
"""
client.username_pw_set(username, password)

"""
connecting to the cloud broker service
"""
client.connect(host, 8883, 60)

"""
calling loop function
"""
client.loop_start()
i = 0
message = "message "
while True:
i = i+1
client.publish("topic", message+str(i), 1)
sleep(3)

client.disconnect()
client.loop_stop()

Comments

  • Tamimi
    Tamimi Member, Administrator, Employee Posts: 541 admin

    Thanks for sharing this @ludobar ! I wrote a blog post about using python mqtt to connect to a solace broker but I did not do a tls configuration so this is great!

    If you're interested in Python, have you checked out our new native Python API?

  • ludobar
    ludobar Member Posts: 10

    @Tamimi said:
    Thanks for sharing this @ludobar ! I wrote a blog post about using python mqtt to connect to a solace broker but I did not do a tls configuration so this is great!

    If you're interested in Python, have you checked out our new native Python API?

    Hi @Tamimi ! I am planning to check your Python API in the next days. I will give you a feedback! :)

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 963 admin

    Awesome! Thanks for sharing this @ludobar !

This Week's Leaders