0

this is my code, it connects, does 1 publish and then reads the messages that come up in my localhost server.

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()

the error im getting is this one

connecting to broker  localhost
subscribing 
publishing 
received message = on
Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\simba\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\simba\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 3452, in _thread_main
    self.loop_forever(retry_first_connection=True)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 2266, in _packet_read
    byte = self._sock_recv(1)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 664, in _sock_recv
    return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'

and sometimes even this one, it really depends

connecting to broker  localhost
subscribing 
publishing 
received message = on
Traceback (most recent call last):
  File "C:/Users\simba/PycharmProjects/hivemqq/main.py", line 27, in <module>
    client.loop_forever()
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1779, in loop_forever
    rc = self.loop(timeout, max_packets)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1181, in loop
    rc = self.loop_read(max_packets)
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 1572, in loop_read
    rc = self._packet_read()
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 2295, in _packet_read
    data = self._sock_recv(self._in_packet['to_process'])
  File "C:\Users\simba\PycharmProjects\hivemqq\venv\lib\site-packages\paho\mqtt\client.py", line 664, in _sock_recv
    return self._sock.recv(bufsize)
AttributeError: 'NoneType' object has no attribute 'recv'

Process finished with exit code 1

lastly, it will sometimes connect succesfully with a single publish but then will not read messages that are incoming, and sometimes everything works, until it doesnt(random everytime). it will then give one of those two errors. any help would be very appreciated.

displayer
  • 1
  • 4
  • i am using pycharm btw, the community edition – displayer Apr 14 '21 at 02:38
  • code works for me when I connect with broker ([mosquitto](https://mosquitto.org/download/)) on other computer (`Raspberry Pi`) – furas Apr 14 '21 at 06:47
  • code works also with local broker `hivemq`. If you run broker in console then you should check if `broker` displays any error message. – furas Apr 14 '21 at 07:12
  • using Google `AttributeError: 'NoneType' object has no attribute` I found this: [AttributeError: 'NoneType' object has no attribute 'recv' #345](https://github.com/eclipse/paho.mqtt.python/issues/345) – furas Apr 14 '21 at 07:13
  • furas, what ide are you using for it to work? – displayer Apr 14 '21 at 14:39
  • I don't use `IDE`. I write code in normal text editor and run it directly in `console/terminal` - `python script.py`. But I use `Linux` on all my computers (Laptop, Raspberry Pi, other). Error shows problem with `socket` and maybe problem makes some firewall settings on `Windows`. I would run `broker` in `console/terminal` to see if it displays any error message when client is connected. – furas Apr 14 '21 at 16:29

1 Answers1

0

remove client.loop_start() loop_forever() doesnot need loop.start() then it will work fine