Paho MQTT on_message返回有趣的消息-python [英] paho MQTT on_message returning a funny message - python

查看:246
本文介绍了Paho MQTT on_message返回有趣的消息-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供一些帮助:) 我刚刚开始在python中玩MQTT. 当我运行以下程序时:

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是消息,但始终显示b''部分. 一旦我在现成的客户端上打开相同的消息,该消息就可以了...所以我认为问题出在代码中,但是我找不到位置.

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.

有任何帮助或指示吗?

谢谢!

推荐答案

正如Moses Koledoye所说,b代表字节-这意味着您要打印的是一组字节的字符串版本.如果将str(msg.payload)更改为msg.payload,则会得到不同的输出.

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.

但是您还没有讨论消息有效负载是什么,因此打印msg.payload仍然可能有些混乱.例如,如果正在发送的消息实际上是一个字节字符串...

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...

这篇关于Paho MQTT on_message返回有趣的消息-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆