订阅具有TLS加密(PYTHON)的mqtt主题

时间:2019-07-12 08:55:22

标签: python-3.x ssl mqtt tls1.2 paho

我有几台服务器通过mqtt代理进行通信,但是为了确保它们之间的通信安全,我使用tls保护了mqtt代理,并且通过这些服务器的专用接口,我可以对其进行配置,以便他可以使用mqqt代理

这是我的mqtt文件夹:

enter image description here

enter image description here

enter image description here

这是我的一台服务器进行配置以加密和解密到mqtt代理的示例:

enter image description here

因此,我有一个通过mqtt与其中一台服务器进行通信的python脚本,但是现在我已经保护了mqtt代理,因此无法与我的脚本进行通信,我还必须在脚本python中添加tls:

import paho.mqtt.client as mqtt #import the client1
import time
import json
import requests
import ssl


#phy = "" #phypayload extrait de la tram


def on_log(client, userdata, level, buf) :

    #print("log: "+buf)
    k=0
#--------------------------------------------------------------------------
def on_connect(client, userdata, flags, rc) :

  if rc==0:
       print("connected ok")
  else:
       print("not connected", rc)
 #---------------------------------------------------------------------------
 def on_disconnect(client, userdata, flags, rc=0) :
    print("disconnect result code "+str(rc))
 #-------------------------------------------------------------------------
 def on_message(client,userdata,msg) :

   global m_decode
   topic=msg.topic
   m_decode=str(msg.payload.decode("utf-8","ignore")) 

   print("message received :  ",m_decode)
   print('')
   print("send to paclido server :")
   print('')
   print("PHYPAYLOAD",phy)


    #---------------------------------------------------------------------


   broker_address="127.0.0.1:1883"

   client = mqtt.Client("paclido") #create new instance
   client.on_connect=on_connect
   client.on_disconnect=on_disconnect
   client.on_log=on_log
   client.on_message=on_message

   #######can't configure tls ? how i'm suppose to do ? ##########
   client.tls_set('/etc/mosquitto/certs/ca/ca.crt', tls_version=1)
   client.tls_insecure_set(True)
   ###############################################################

   print ("cnct to broker", broker_address)
   client.connect("127.0.0.1", 8883, 60)
   client.subscribe([("gateway/abcdef1010101010/rx", 0)("gateway/a10b20c20d30e40f/rx", 0)])
   client.loop_forever()

您可以看到在我的代码中我尝试配置tls,但是它不起作用,我看到了文档,但是我不能,这是我的python的日志:

enter image description here

有人可以帮助我配置python脚本吗?谢谢 !

0 个答案:

没有答案