为什么我不能在 Raspberry Pi 上使用 python-mosquitto?

Why can't I use python-mosquitto on the Raspberry Pi?

我正在使用带有 Debian Wheezy 映像的 Raspberry Pi。我已经安装了 Mosquitto(MQTT 协议的代理)、mosquitto 客户端和 python mosquitto 以便在我的 Python 脚本中使用 mosquitto,我有 运行 一个非常简单的例子来测试我所有的包是否工作好不好

import mosquitto
mqttc = mosquitto.Mosquitto("python_pub")
mqttc.will_set("/event/dropped", "Sorry, I seem to have died.")
mqttc.connect("127.0.0.1", 1883, 60, True)

mqttc.publish("hello/world", "Hello, World!")

出于某种原因,我收到以下错误。

Traceback (most recent call last):
File "test_1.py", line 1, in <module>
import mosquitto
File "/usr/lib/pymodules/python2.7/mosquitto.py", line 484, in <module>
_mosquitto_log_init = _libmosq.mosquitto_log_init
File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
func = self.__getitem__(name)
File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /usr/lib/libmosquitto.so.1: undefined symbol: mosquitto_log

有人可以解释为什么这不起作用以及可能的解决方案。

The Mosquitto Python module has been donated to the Eclipse Paho project. It can be installed using “pip install paho-mqtt” and there is documentation available at https://pypi.python.org/pypi/paho-mqtt

Existing users of the Mosquitto Python module should find it very easy to port their code to the Paho version.

http://mosquitto.org/documentation/python/

来自https://eclipse.org/paho/clients/python/的官方示例:

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc):
    print("Connected with result code "+str(rc))
    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

试试吧!

我实际上正在为我的大学开发一个项目,使用 mosquitto 作为 MQTT 的代理。我建议您使用 paho 作为 python 模块来使用 MQTT 发布和订阅。

官方页面:

https://pypi.python.org/pypi/paho-mqtt

这是一个非常简单的示例,它订阅了代理 $SYS 主题树并打印出结果消息(取自 https://pypi.python.org/pypi/paho-mqtt):

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("$SYS/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("iot.eclipse.org", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()