在基于MQTT实现Android消息推送(Push) 中介绍了android客户端使用MQTT的示例代码 AndroidPushNotificationsDemo
有一个Mosquitto,实现Server/broker的网站,可以用来作为测试网站,使用这个网站发送消息的示例代码如下。
#!/usr/bin/env python
#coding=utf-8
import mosquitto
import os
import time
broker = "test.mosquitto.org"
port = 1883
mypid = os.getpid()
client_uniq = "pubclient_"+str(mypid)
mqttc = mosquitto.Mosquitto(client_uniq)
#connect to broker
mqttc.connect(broker, port, 60, True)
#remain connected and publish
while mqttc.loop() == 0:
msg = "test message "+time.ctime()
mqttc.publish("topic/deviceId", msg)
print "message published"
time.sleep(1)
pass