Python 中的 MQTT,订阅者未响应

MQTT in Python, subscriber not responding

关注 tutorial 关于在 Python 中实施 MQTT 的精彩内容。当我 运行 发布者脚本但挂在订阅者脚本时工作正常。我 运行 在不同的命令行中同时运行这两个脚本。这是我的两个代码:

#pub.py

import paho.mqtt.client as mqtt
import logging
import json
import time
import random
logging.basicConfig(level=logging.INFO)
# use DEBUG, INFO, WARNING

username="xxxxx"
password="xxxxx"
broker_url ='xxxxx'
broker_port=0000
client_id=f"client-{random.randint(0, 100)}"
to_wait=5
topic='hey-hey'
publish_count = 3
msg= 'Hey'

def on_log(client, userdata, level, buf):
    logging.info(buf)
    client.on_log = on_log

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            logging.info("Connected to Broker!: {}".format(rc))
        else:
            logging.info("Failed to Connect with code: "+str(rc))
    
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker_url, broker_port)
    return client

def on_publish(client):
    count = 1
    while count <= publish_count:
        time.sleep(to_wait)
        message = str(msg)
        result = client.publish(topic, message, 1, 1)
        status = result[0]
        if status == 0:
            published= client_id+ ' sent the message: ' +message)
            print(published)
        else:
            print(f"Failed to send the message")
        count +=1
        
def run():
    device1 = connect_mqtt()
    device1.loop_start
    on_publish(device1)
    device1.loop_stop()
    
if __name__ == '__main__':
    run()

我得到:

client-27 sent the message: Hey
client-27 sent the message: Hey
client-27 sent the message: Hey

然而,对于sub.py

import paho.mqtt.client as mqtt
import logging
import json
import time
import random
logging.basicConfig(level=logging.INFO)
from datetime import datetime 
# use DEBUG, INFO, WARNING

username="xxxx"
password="xxxxxx"
broker_url ='xxxxx'
broker_port=0000
client_id=f"client-{random.randint(0, 100)}"
to_wait=5
topic='hey-hey'
publish_count = 3

def on_log(client, userdata, level, buf):
    logging.info(buf)
    client.on_log = on_log

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            logging.info("Connected to Broker!: {}".format(rc))
        else:
            print("Failed to Connect with code: "+str(rc))
            client.loop_stop()
    
    client = mqtt.Client(client_id)
    client.username_pw_set(username, password)
    client.on_connect = on_connect
    client.connect(broker_url, broker_port, 60)
    return client

def subscribe(client):
    def on_subscribe(client,userdata, mid, granted_qos):
        logging.info('subscribed')
        
    client.subscribe(topic)
    client.subscribe = on_subscribe


def process_message(client, userdata, message):
    msgr=str(message.payload.decode('utf-8'))
    msgr='Message Received' + msgr
    logging.info(msgr)
        
def run():
    device2 = connect_mqtt()
    device2.on_message = process_message
    device2.loop_forever()
    
    
if __name__ == '__main__':
    run()

它只是挂起并超时。我错过了什么吗?

您永远不会在订阅者代码中调用 subscribe(),因此它永远不会告诉代理它想要接收什么主题。