开发者社区 > 云原生 > 云消息队列 > 正文

python订阅微消息队列mqtt版,意外断开的问题

用阿里的示例代码,连接总是断开,也没办法订阅和接收:

import hmac import base64 from hashlib import sha1 import time from paho.mqtt.client import MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING, MQTT_LOG_ERR, MQTT_LOG_DEBUG from paho.mqtt import client as mqtt

client_id='XXXXXX'

topic = 'al001'

brokerUrl='XXXXX.mqtt.aliyuncs.com'

def on_connect(client, userdata, flags, rc): print('Connected with result code ' + str(rc)) client.subscribe(topic, 0) for i in range(1, 11): print(i) rc = client.publish(topic, str(i), qos=0) print ('rc: %s' % rc) time.sleep(0.1) def on_message(client, userdata, msg): print(msg.topic + ' ' + str(msg.payload))

def on_disconnect(client, userdata, rc): if rc != 0: print('Unexpected disconnection %s' % rc)

client = mqtt.Client(client_id, protocol=mqtt.MQTTv311, clean_session=True) client.on_connect = on_connect client.on_message = on_message client.on_disconnect = on_disconnect

userName = "XXX" password = "XXX"

client.connect(brokerUrl, 1883, 60) client.loop_forever()

结果显示: Connected with result code 0 1 rc: (0, 2) 2 rc: (0, 3) 3 rc: (0, 4) 4 rc: (0, 5) 5 rc: (0, 6) 6 rc: (0, 7) 7 rc: (0, 8) 8 rc: (0, 9) 9 rc: (0, 10) 10 rc: (0, 11) Unexpected disconnection 7

展开
收起
游客odfnlsnclcrpg 2021-11-08 17:17:48 878 0
0 条回答
写回答
取消 提交回答

涵盖 RocketMQ、Kafka、RabbitMQ、MQTT、轻量消息队列(原MNS) 的消息队列产品体系,全系产品 Serverless 化。RocketMQ 一站式学习:https://rocketmq.io/

相关产品

  • 云消息队列 MQ
  • 热门讨论

    热门文章

    相关电子书

    更多
    From Python Scikit-Learn to Sc 立即下载
    Data Pre-Processing in Python: 立即下载
    双剑合璧-Python和大数据计算平台的结合 立即下载