paho MQTT on_message returning a funny message - python

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

some help please :) I just started to play with MQTT in python. When I run the following program:

import paho.mqtt.client as mqtt   def on_connect(client, userdata, flags, rc):     print("Connected with result code "+str(rc))     client.subscribe("watchdog/#")  def on_message(client, userdata, msg):     message = str(msg.payload)     print(msg.topic+" "+message)  client = mqtt.Client() client.username_pw_set('XXXX', password='XXXXXXX') client.on_connect = on_connect client.on_message = on_message  client.connect("XXXX", XXXXX, 60)  client.loop_forever() 

the payload always have the following text:

b'XXX'

XXX is the message, but the b' ' part ALWAYS appear. once i open the same message on off the shelf client, the message is fine... so i assume the problem is in the code, but i cannot find where.

any help or directions?

thanks!

回答1:

As Moses Koledoye says, b is for bytes - this means that what you are printing is the string version of a set of bytes. If you changed the str(msg.payload) to simply msg.payload, you will get a different output.

But you haven't talked about what the message payload is, so you may still get gibberish out printing the msg.payload. For example, if the message being sent is actually a string of bytes...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!