# -*- coding: utf-8 -*-
import pika
import sys

user_info = pika.PlainCredentials('user', 'J70e6K7BRrxrU1dO')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('rabbitmq.rabbitmq.svc.cluster.local', 5672, '/', user_info)
)
channel = connection.channel()

# 1. 声明 topic 交换机
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

# 2. 从命令行获取 routing_key 和消息
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
message = ' '.join(sys.argv[2:]) or 'Hello Topic!'

channel.basic_publish(
    exchange='topic_logs',
    routing_key=routing_key,
    body=message
)
print(f" [x] Sent {routing_key}:{message}")
connection.close()