簡體   English   中英

嘗試使用 paho python 將 mosquitto 代理與 TLS 一起使用

[英]Trying to use mosquitto broker with TLS using paho python

蟒蛇代碼

import time
broker = "test.mosquitto.org"
port=8884
conn_flag= False
def on_connect(client, userdata, flags, rc):
    global conn_flag
    conn_flag=True
    print("connected",conn_flag)
    conn_flag=True
def on_log(client, userdata, level, buf):
    print("buffer", buf)
def on_disconnect(client, userdata, rc):
    print("client disconnected ok")
client1= paho.Client("control")
client1.on_log=on_log
client1.tls_set('C:\etc\mosquitto\certs\mosquitto.org.crt')
client1.on_connect = on_connect
client1.on_disconnect = on_disconnect
client1.connect(broker,port)
while not conn_flag:
    time.sleep(1)
    print("waiting", conn_flag)
    client1.loop()
time.sleep(3)
print("publishing")
client1.publish("house/bulb", "Test")
time.sleep(2)
client1.loop()
time.sleep(2)
client1.disconnect()

我正在使用 test.mosquitto.org 提供的 mosquitto.org.crt(PEM 格式)文件,目前無法連接到端口 8884,conn_flag 始終為 false,我該怎么辦?

根據http://test.mosquitto.org ,端口是:

8883:MQTT,加密,未經身份驗證
8884:MQTT,加密,需要客戶端證書

在您的代碼client1.tls_set('C:\etc\mosquitto\certs\mosquitto.org.crt')中,您正在設置ca_cert - 參數為:

tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLS, ciphers=None)

這足以連接到端口8883 (並且您的代碼對我來說成功連接到該端口)。 8883端口的連接將被加密,客戶端可以確認服務器的身份; 但是客戶端不必提供客戶端證書(以標識自己)。

要連接到端口8884 ,您必須提供客戶端證書(用於驗證客戶端) - 即certfilekeyfile參數。 可在此處申請適當的證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM