简体   繁体   中英

Python Paho MQTT client not instantiated

I want to follow the example mentioned here to connect to a broker in Python. The code is just stuck at the instantiation part. No response, no nothing ! What could be wrong ?

pi@raspberrypi ~ $ python
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import paho.mqtt.client as mqtt
>>> c = mqtt.Client("random")

The mqtt.Client doesn't ever complete. Since the connect method is yet to be called, I am not sure, if this could be a network issue. Nevertheless, a Ctrl-C shows the following:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py",     line 422, in __init__
self._sockpairR, self._sockpairW = _socketpair_compat()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 271, in _socketpair_compat
    sock2, address = listensock.accept()
  File "/usr/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
KeyboardInterrupt
>>>

This happens with python3 too.

UPDATE : This issue happens on the Raspberry Pi. On Ubuntu, the same code works fine.

Looks like networking ( sock ) not properly configured. Did you test your Raspberry Pi networking?

Test that you receive answers from: ping 8.8.8.8

Test dns with: ping www.google.com

Check that you have a valid gateway with: ip route show

Check in your /etc/resolv.conf that there are valid entries like:

nameserver 8.8.8.8
nameserver 8.8.4.4

Try also to call Client method without arguments or with all of them:

c = mqtt.Client()

c = mqtt.Client(client_id="123456789", clean_session=True, userdata=None, protocol=MQTTv311)

I just had the exact same issue - running client.py ( /usr/local/lib/python2.7/dist-packages/paho/mqtt/ ) manually it would hand up at:

271     sock2, address = listensock.accept()

ping localhost would fail (or would ping some host out on the internet, localhost.home.net ;) Turns out I had 127.0.0.1 localhost commented out in /etc/hosts :

 #127.0.0.1   localhost

Once I removed the hash it started working OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM