我使用带有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
有人可以解释为什么这不起作用并且可能解决方案。
答案 0 :(得分:3)
我实际上正在使用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()
答案 1 :(得分:2)
Mosquitto Python模块已捐赠给Eclipse Paho项目。可以使用“pip install paho-mqtt”进行安装,https://pypi.python.org/pypi/paho-mqtt
提供文档Mosquitto Python模块的现有用户应该发现将代码移植到Paho版本非常容易。
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()
试一试!