在python中有任何支持在mqtt代理上订阅端口8080
import sys
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
mqttc = mqtt.Client()
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("test.mosquitto.org", 8080, 60)
mqttc.subscribe("test/iot", 0)
mqttc.loop_forever()
我无法连接此代码。 Mosquitto在端口8080上有websocket支持,但是这个paho库不能用于它。任何python的解决方案? 我在Windows 10上使用python 2.7。
答案 0 :(得分:4)
Paho MQTT模块几天前引入了websocket支持。我认为它尚未发布,但您可以使用
从Linux分支机构下安装function my_scripts_loader() {
wp_enqueue_script('myjquerylib-js', get_template_directory_uri() . '/assets/js/myjquerylib.js');
}
add_action('wp_enqueue_scripts', 'my_scripts_loader');
也适用于Windows。 (感谢评论中的信息)
您可以通过连接
将websockets用作传输pip install git+git://github.com/eclipse/paho.mqtt.python.git
<强>更新强>
如果您尝试将websocket协议与python客户端一起使用,因为您还需要连接浏览器客户端(例如MQTT.js),那么您还可以配置mosquitto来监听 websockets 和正常 mqtt协议。
只需创建一个配置文件,例如
mqttc = mqtt.Client(transport="websockets")
具有以下内容:
/etc/mosquitto/mosquitto.conf
然后你可以用
运行mosquittolistener 1883
protocol mqtt
listener 9001
protocol websockets
你应该看到类似的输出:
mosquitto -c /etc/mosquitto/mosquitto.conf
然后您的python客户端连接到端口1883,浏览器客户端连接到9001
您可以使用what-mqtt browser client来测试websocket侦听器。只需将其指向1469015320: mosquitto version 1.4.8 (build date 2016-05-3112:07:40+0200) starting
1469015320: Config loaded from /etc/mosquitto/mosquitto1.conf.
1469015320: Opening ipv4 listen socket on port 1883.
1469015320: Opening ipv6 listen socket on port 1883.
1469015320: Opening websockets listen socket on port 9001.
答案 1 :(得分:3)
2月到现在,paho.mqtt.python代码库已得到修复。只需添加transport='websockets'
作为Fl0v0建议,它就可以了。以下是订阅$SYS/
下所有内容或全部内容的完整代码。测试的代码在2016年6月3日的主分支上使用提交哈希e56f913
。
#!/usr/bin/python
import sys
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, rc):
print("rc: "+str(rc))
def on_message(mqttc, obj, msg):
print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
def on_publish(mqttc, obj, mid):
print("mid: "+str(mid))
def on_subscribe(mqttc, obj, mid, granted_qos):
print("Subscribed: "+str(mid)+" "+str(granted_qos))
def on_log(mqttc, obj, level, string):
print(string)
mqttc = mqtt.Client(transport='websockets')
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
mqttc.connect("test.mosquitto.org", 8080, 60)
mqttc.subscribe("#", 0)
#mqttc.subscribe("$SYS/#", 0)
mqttc.loop_forever()
更新:通过从一个版本的Cygwin(不是cygwin python)调用,上面的代码在我测试过的带有python 2.7.6的Windows 7上不起作用。主题"#"
或"$SYS/#"
都没有给出任何已发布的消息。
相反,pub或sub到solid主题的工作方式如下所示。修补程序paho.mqtt.python examples/pub-single.py
和examples/sub-simple.py
。然后在一个终端中运行sub-simple.py
。在另一个终端中运行pub-single.py
两次。订户终端将打印出发布者发布的两条消息。
补丁到examples/pub-single.py
:
-publish.single("paho/test/single", "boo", hostname="test.mosquitto.org")
+publish.single("/HelloWorld", "boo", hostname="test.mosquitto.org",
+ port=8080, transport='websockets')
补丁到examples/sub-simple.py
:
-topics = ['#']
+topics = ['/HelloWorld']
-m = subscribe.simple(topics, hostname="iot.eclipse.org", retained=False, msg_count=2)
+m = subscribe.simple(topics, hostname="test.mosquitto.org",
+ retained=False, msg_count=2,
+ port=8080, transport='websockets')
答案 2 :(得分:1)
不,Paho库的Python版本不支持基于Websockets的MQTT。
事实上,我认为唯一支持基于Websockets的MQTT的本机客户端(不在浏览器中)MQTT库是NodeJS模块(https://www.npmjs.com/package/mqtt)