Python PAHO MQTT无法使用用户名和密码连接到mqtts

2022-03-28 00:00:00 python mqtt

问题描述

我的raspbercrypi上的以下python代码没有连接到我的MQTT代理,它只是在打印后挂起Connecting...

import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("test")


def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))


client = mqtt.Client(client_id="",clean_session=True,userdata=None,protocol=mqtt.MQTTv311,transport="tcp")
client.on_connect = on_connect
client.on_message = on_message

client.username_pw_set(username="stackoverflow",password="stackoverflow")
print("Connecting...")
client.connect("learn.evermight.net", 9101, 10)
client.loop_forever()

我的Python代码哪里做错了?


命令行成功

我确认我的MQTT工作正常,因为我可以使用以下命令从终端订阅:

mosquitto_sub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" --capath /etc/ssl/certs/

只要我从另一个终端运行此命令,我就会在终端中看到消息

mosquitto_pub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" -m "hello world" --capath /etc/ssl/certs/

我的python代码有什么问题?


NodeJS成功

此外,以下NodeJS代码也可用于连接和发布到我的MQTT服务器。

const mqtt = require('async-mqtt');

  try{
    const client = await mqtt.connectAsync("mqtts://learn.evermight.net",{
      port:9101,
      host:"mqtts://learn.evermight.net",
      username:"stackoverflow",
      password:"stackoverflow",
      connectTimeout:5000,
      protocolId:"MQIsdp",
      protocolVersion:3,
      encoding:"utf8",
      keepalive: 60
    });
    await client.publish("test","hello world");
    await client.end();
  } catch(e) {
    console.log(e);
  }

网站JavaScript成功

以下代码还可以通过Web浏览器javascript网站连接到WebSocket端口,订阅test主题并接收发布的消息(请注意,我的WebSockets使用端口9102)

import Paho from "paho-mqtt";

const client = new Paho.Client("learn.evermight.net",9102,"WebBrowser");
  client.onConnectionLost = response=>console.log("lostMQTTConnection: " +(response.errorCode !== 0 ? response.errorMessage : "Unknown MQTT Error" ));
  client.onMessageArrived = message=>console.log(message.payloadString);
  client.connect({
    onSuccess:_=>client.subscribe("test"),
    useSSL:true,
    userName:"stackoverflow",
    password:"stackoverflow",
  });

解决方案

我发现如果添加不带参数的此命令就可以连接

client.tls_set()

在paho documentation中tls_set()的说明末尾,您可以看到

Must be called before connect*().

但它在client.connect()

之后也适用于我

在同一个domentation中,您可以看到,在没有参数的情况下,它使用默认的系统设置

By default, on Python 2.7.9+ or 3.4+,  
the default certification authority of the system is used. 

On older Python version this parameter is mandatory.

仅当mosquitto_sub/mosquitto_pub需要--capath /etc/ssl/certs/时才需要。
如果mosquitto_sub/mosquitto_pub在没有--capath /etc/ssl/certs/的情况下工作,则不要使用它。


import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
    print("Connected with result code", rc)
    client.subscribe("test")

def on_message(client, userdata, msg):
    print(msg.topic, msg.payload)


client = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp")
client.on_connect = on_connect
client.on_message = on_message

client.tls_set()  # <--- even without arguments

client.username_pw_set(username="stackoverflow", password="stackoverflow")
print("Connecting...")
client.connect("learn.evermight.net", 9101, 10)
client.loop_forever()

相关文章