简体   繁体   中英

When the paho python mqtt client gets a message from subscribed topic, it always adds a “b” to the received message

#!/usr/bin/env python3

import paho.mqtt.client as mqtt

client.subscribe("test/#")

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

client = mqtt.Client()
client.on_message = on_message
client.connect("localhost", 1883, 60)

client.loop_forever()

When I receive a message it always adds a "b" to the message: test/temperature b 'test'

https://www.dinotools.de/2015/04/12/mqtt-mit-python-nutzen/

I got the solution: "bXXX" means bytes. You need to convert this to UTF-8 before using it:

msg.payload = msg.payload.decode("utf-8")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM