保持(python)paho MQTT连接不确定地存活

问题描述 投票:0回答:1

当前,我正在使用以下功能将其发布到MQTT代理:

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

def publish_to_broker(data):
    publish.multiple(data, hostname=example_host, port=8883, client_id="example_id", keepalive=60,
                     will=None, auth={'username': "example_user", 'password': "example_password"},
                     tls={'ca_certs': certifi.where(), 'tls_version': ssl.PROTOCOL_TLSv1_2}, 
                     protocol=mqtt.MQTTv311, transport="tcp")

由于这是为每个帖子建立一个新的连接,所以我需要更改它以使其保持活动状态,并且如果连接丢失则重新连接。

[可悲的是,我发现文档非常无用,例如tls=部分仅与上面的方法一起使用,其中mqtt连接在发布后立即关闭。但是,我想更像是这样做:

mqtt_client = mqtt.Client(client_id="example_id", protocol=mqtt.MQTTv311, transport="tcp")

mqtt_client.connect(host=example_host, port=8883, auth={'username': "example_user", 'password': "example_password"},  tls={'ca_certs': certifi.where(), 'tls_version': ssl.PROTOCOL_TLSv1_2})


def publish_to_broker_smart(data):
    mqtt_client.publish(data)

但是,我收到错误消息:TypeError: connect() got an unexpected keyword argument 'auth'

这些规格在哪里适合?

python mqtt paho
1个回答
0
投票

tls和auth必须指定不同。这是要走的路:

© www.soinside.com 2019 - 2024. All rights reserved.