# Python program connecting over TLS/SSL to PubSub+ Cloud

**URL:** <https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463>\
**Category:** Training, Tips and Tricks\
**Created:** [October 24, 2020, 2:15pm UTC](https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463 "2020-10-24T14:15:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ludobar](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@ludobar](https://community.solace.com/u/ludobar)\
**Post date:** [October 24, 2020, 2:15pm UTC](https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463/1 "2020-10-24T14:15:32Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![Tamimi](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/tamimi/32/2190_2.png) [@Tamimi](https://community.solace.com/u/Tamimi)\
**Post date:** [October 26, 2020, 2:10pm UTC](https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463/2 "2020-10-26T14:10:38Z")

</div>

Thanks for sharing this @ludobar ! I wrote a [blog post](https://dev.to/solacedevs/how-i-used-covid-tracking-data-to-build-and-learn-a-python-event-driven-application-1l44) 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?

---

<div class="post-metadata">

**Author:** ![ludobar](https://avatars.discourse-cdn.com/v4/letter/l/47e85d/32.png) [@ludobar](https://community.solace.com/u/ludobar)\
**Post date:** [October 29, 2020, 11:49am UTC](https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463/3 "2020-10-29T11:49:52Z")

</div>

> [@Tamimi](#):
>
> Thanks for sharing this @ludobar ! I wrote a [blog post](https://dev.to/solacedevs/how-i-used-covid-tracking-data-to-build-and-learn-a-python-event-driven-application-1l44) 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! 🙂

---

<div class="post-metadata">

**Author:** ![marc](https://yyz1.discourse-cdn.com/solace/user_avatar/community.solace.com/marc/32/2191_2.png) [@marc](https://community.solace.com/u/marc)\
**Post date:** [October 30, 2020, 6:39pm UTC](https://community.solace.com/t/python-program-connecting-over-tls-ssl-to-pubsub-cloud/463/4 "2020-10-30T18:39:10Z")

</div>

Awesome! Thanks for sharing this @ludobar !
