这是我的代码,它连接、发布1个消息,然后读取本地主机服务器中出现的消息。
import time
import paho.mqtt.client as paho
broker="localhost"
def on_message(client, userdata, message):
time.sleep(1)
print("received message =", str(message.payload.decode("utf-8")))
client = paho.Client("client-001")
client.on_message = on_message
print("connecting to broker ", broker)
client.connect(broker, port=1883)#connect
client.loop_start() #start loop to process received messages
print("subscribing ")
client.subscribe("anyisfine")#subscribe
time.sleep(2)
print("publishing ")
#client.publish("anyisfine", "on")#publish
client.publish("anyisfine", "on")#publish
time.sleep(4)
client.loop_forever()
我得到的错误是这一个
connecting to broker localhost
subscribing
publishing
received message = on
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:UserssimbaAppDataLocalProgramsPythonPython38libthreading.py", line 932, in _bootstrap_inner
self.run()
File "C:UserssimbaAppDataLocalProgramsPythonPython38libthreading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 3452, in _thread_main
self.loop_forever(retry_first_connection=True)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1572, in loop_read
rc = self._packet_read()
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 2266, in _packet_read
byte = self._sock_recv(1)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 664, in _sock_recv
return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'
有时甚至是这一次,它真的取决于
connecting to broker localhost
subscribing
publishing
received message = on
Traceback (most recent call last):
File "C:/Userssimba/PycharmProjects/hivemqq/main.py", line 27, in <module>
client.loop_forever()
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 1572, in loop_read
rc = self._packet_read()
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 2295, in _packet_read
data = self._sock_recv(self._in_packet['to_process'])
File "C:UserssimbaPycharmProjectshivemqqvenvlibsite-packagespahomqttclient.py", line 664, in _sock_recv
return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'
Process finished with exit code 1
最后,它有时会成功地连接到一个发布,但不会读取传入的消息,有时一切都正常,直到它不正常为止(每次都是随机的(。然后它将给出这两个错误中的一个。任何帮助都将不胜感激。
remove client.lop_start((loop_forvers((不需要loop.start((那么就会正常工作