MQTT入门(2)- 公开的服务Broker

使用MQTT,前提是需要一个服务器Server/Broker,除了自己搭建服务,刚开始时可以使用第三方提供的在线公开的免费Broker服务。使用这些免费Broker服务,可以在刚开始时快速理解MQTT协议。

[img]http://dl2.iteye.com/upload/attachment/0128/3608/e523c06b-0ad1-3186-b4bc-3288f944c410.png[/img]
其他:https://github.com/mqtt/mqtt.github.io/wiki/public_brokers

这些Broker/Server已经搭建好了,只需要编写Client/Subscriber就能测试一下MQTT了。

以使用Python的paho-mqtt来实现客户端。连接test.mosquitto.org服务为例。

[b](1)安装Eclipse Paho客户端[/b]
pip install paho-mqtt


[b](2)客户端连接[/b]

unencrypted-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test/rensanning/time")

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("test.mosquitto.org", 1883)
client.loop_forever()


执行 python unencrypted-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3610/98519e1c-c7f0-3fee-836c-53dd850911e8.png[/img]

[b](2)发布消息[/b]

pub-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))

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("test.mosquitto.org", 1883)

while client.loop() == 0:
msg = "test message from Publisher "+time.ctime()
client.publish("test/rensanning/time", msg, 0, True)
print("message published")
time.sleep(1.5)
pass


执行 python pub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3612/9133af4f-1fb9-3c33-988e-bf7f2f0c2c92.png[/img]

[b](3)订阅消息[/b]

sub-mqtt.py
# coding=utf8
import paho.mqtt.client as mqtt

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test/rensanning/time")

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("test.mosquitto.org", 1883)
client.loop_forever()


执行 python sub-mqtt.py
[img]http://dl2.iteye.com/upload/attachment/0128/3614/1dbeee16-0b28-329a-b940-db96b3e10a89.png[/img]

[b]同时运行订阅和发布客户端。[/b]
[img]http://dl2.iteye.com/upload/attachment/0128/3616/a55ed0db-97b0-3e24-b459-1917d38bffdd.png[/img]

[b]加密通信,访问8883端口[/b]

先从 http://test.mosquitto.org/ 下载 mosquitto.org.crt文件。

pub-mqtt-tls.py
# coding=utf8
import paho.mqtt.client as mqtt
import time

def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))

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.tls_set("mosquitto.org.crt")
client.connect("test.mosquitto.org", 8883)

while client.loop() == 0:
msg = "test message from Publisher "+time.ctime()
client.publish("test/rensanning/time", msg, 0, True)
print("message published")
time.sleep(1.5)
pass


执行 python pub-mqtt-tls.py
[img]http://dl2.iteye.com/upload/attachment/0128/3618/6ce405f8-1f6f-3907-8b03-13d76e01f583.png[/img]
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值