modify: mqtt broker 适配
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
MQTT Publisher — 向 chongming Worker 发送 MQTT 请求
|
||||
|
||||
用法:
|
||||
python publisher.py <worker_type> <operation> [json_payload]
|
||||
|
||||
示例:
|
||||
python publisher.py example add '{"a": 3, "b": 5, "_method": "GET"}'
|
||||
python publisher.py doubao analyze '{"text": "hello", "_method": "POST"}'
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import json
|
||||
import logging
|
||||
from amqtt.client import MQTTClient
|
||||
from amqtt.mqtt.constants import QOS_1
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# MQTT 主题前缀(与 mqtt_bridge.py 保持一致)
|
||||
PREFIX = "chongming"
|
||||
|
||||
# 默认参数
|
||||
DEFAULT_WORKER = "example"
|
||||
DEFAULT_OP = "add"
|
||||
DEFAULT_PAYLOAD = {"a": 3, "b": 5, "_method": "GET"}
|
||||
|
||||
async def main():
|
||||
worker_type = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_WORKER
|
||||
operation = sys.argv[2] if len(sys.argv) > 2 else DEFAULT_OP
|
||||
payload_str = sys.argv[3] if len(sys.argv) > 3 else json.dumps(DEFAULT_PAYLOAD)
|
||||
|
||||
topic = f"{PREFIX}/{worker_type}/{operation}"
|
||||
payload = json.loads(payload_str)
|
||||
|
||||
client = MQTTClient()
|
||||
await client.connect("mqtt://localhost:1883/")
|
||||
|
||||
await client.publish(topic, json.dumps(payload).encode("utf-8"), QOS_1)
|
||||
print(f"[Publisher] Sent to '{topic}': {payload}")
|
||||
|
||||
await client.disconnect()
|
||||
print("[Publisher] Disconnected")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user