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

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

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

# 2. 构造 headers
headers = {
    'x-version': '1',
    'x-env': 'prod',
    'x-critical': 'true'
}

message = ' '.join(sys.argv[1:]) or 'Hello Headers!'

channel.basic_publish(
    exchange='header_logs',
    routing_key='',                      # headers 交换机忽略 routing_key
    body=message,
    properties=pika.BasicProperties(headers=headers)
)
print(f" [x] Sent {headers} -> {message}")
connection.close()