paho.mqtt + python TypeError: a bytes-like object is required, not 'str'

paho.mqtt + python TypeError: a bytes-like object is required, not 'str'

在 Python 中创建了两个 paho.mqtt 客户端(client1 和 client2),它们连接到 mosquitto 代理。两个客户订阅了不同的主题。客户端 1 运行s loop_forever(),客户端 2 运行s loop_start()。 client1 的 on_message() 回调将消息存储在 MySQL 数据库中。 client2 的 on_message() 回调 运行 是一个简单的测试。

测试包含一个使用模拟对象的测试方法paho.mqtt.client.MQTTMessage。对于 client2,我在命令行中发布来自 mosquitto_pub 的消息。当我收到 运行 测试的消息时,测试 运行s 成功了。但在那之后我收到以下错误消息。当我评论使用此模拟对象 paho.mqtt.client.MQTTMessage 进行测试的测试方法时,我没有收到此错误消息。

这个错误是来自 Python 还是 paho.mqtt?还是来自 client1.loop_forever() 方法?

File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1481, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1003, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1284, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 1849, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2305, in _packet_handle
return self._handle_publish()
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2500, in _handle_publish
self._handle_on_message(message)
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/client.py", line 2640, in _handle_on_message
for callback in self._on_message_filtered.iter_match(message.topic):
File "/usr/local/lib/python3.6/site-packages/paho_mqtt-1.3.1-py3.6.egg/paho/mqtt/matcher.py", line 60, in iter_match
lst = topic.split('/')
TypeError: a bytes-like object is required, not 'str'

如 paho 文档中所述,您的主题必须是 bytes 类型:

class MQTTMessage(object):
""" This is a class that describes an incoming or outgoing message. It is
passed to the on_message callback as the message parameter.

Members:

topic : String/bytes. topic that the message was published on.
payload : String/bytes the message payload.
qos : Integer. The message Quality of Service 0, 1 or 2.
retain : Boolean. If true, the message is a retained message and not fresh.
mid : Integer. The message id.

On Python 3, topic must be bytes.
"""

__slots__ = 'timestamp', 'state', 'dup', 'mid', '_topic', 'payload', 'qos', 'retain', 'info'

def __init__(self, mid=0, topic=b""):
    self.timestamp = 0
    self.state = mqtt_ms_invalid
    self.dup = False
    self.mid = mid
    self._topic = topic
    self.payload = b""
    self.qos = 0
    self.retain = False
    self.info = MQTTMessageInfo(mid)

def __eq__(self, other):
    """Override the default Equals behavior"""
    if isinstance(other, self.__class__):
        return self.mid == other.mid
    return False

def __ne__(self, other):
    """Define a non-equality test"""
    return not self.__eq__(other)

@property
def topic(self):
    if sys.version_info[0] >= 3:
        return self._topic.decode('utf-8')
    else:
        return self._topic

@topic.setter
def topic(self, value):
    self._topic = value

-> On Python 3、topic必须是bytes。

所以确保你的主题是一个字节对象,然后它应该工作

顺便说一句,这里是字符串和字节转换的简短示例:

mystring = 'hello_string'
b1 = bytes(mystring, 'utf-8')
b2 = str(b1, 'utf-8')
print(b1)
print(b2)

输出为:

b'hello_string'
hello_string