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

标签 python raspberry-pi raspbian mqtt mosquitto

我正在使用带有 Debian Wheezy 镜像的 Raspberry Pi。我已经安装了 Mosquitto(MQTT 协议(protocol)的代理)、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

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

最佳答案

我实际上正在为我的大学开发一个项目,使用 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()

关于python - 为什么我不能在 Raspberry Pi 上使用 python-mosquitto?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447215/

相关文章:

linux - "-sh: executable_path:not found"是什么意思

python - 为什么一个小脚本在执行后会继续使用 70%+ 的 CPU?

python - 代码调试,为什么运行后LED不灭?

python - 如何打印具有数据类型的 numpy 数组?

python - 在 Tornado 应用程序中将阻塞功能卸载到执行程序的通用解决方案

image - VideoCapture.read()返回过去的图像

raspberry-pi - 与树莓派串口通信

python - 如何使用带有多个参数的 Flask Jinja2 url_for

Python ThreadingMixin 和 BaseHTTPServer

linux - 从 ttyUSB0 读取输出