python实现rabbitnq的mqtt订阅
python实现rabbitnq之mqtt订阅
mqtt协议的介绍我这儿就不提了 网上多的是 一查就有了
rabbitnq下载及安装参考:
https://blog.csdn.net/weixin_44015043/article/details/104861596#comments_13065410
此处先得pip install paho-mqtt获得mqtt的包
直接上代码:
-- coding: utf-8 --
以下代码在2019年2月28日 python3.6环境下运行通过
import paho.mqtt.client as mqtt
import json
import time
HOST = “自己rabbitmq的ip”
PORT = 1883
client_id = “” # 没有就不写,此处部分内容用xxx代替原内容,下同
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(“amq.topic”) # 订阅消息
def on_message(client, userdata, msg):
print(“主题:”+msg.topic+" 消息:"+str(msg.payload.decode(‘utf-8’)))
def on_subscribe(client, userdata, mid, granted_qos):
print(“On Subscribed: qos = %d” % granted_qos)
def on_disconnect(client, userdata, rc):
if rc != 0:
print(“Unexpected disconnection %s” % rc)
client = mqtt.Client(client_id)
client.username_pw_set(“mqtt-test”, “mqtt-test”)
client.on_connect = on_connect
client.on_message = on_message
client.on_subscribe = on_subscribe
client.on_disconnect = on_disconnect
client.connect(HOST, PORT, 60)
client.publish(“amq.topic”, ) # 发送消息
client.loop_forever()
本文地址:https://blog.csdn.net/Chs_12/article/details/108238996
上一篇: 抖音卡字怎么制作 抖音卡字制作方法介绍