Files
smart-management-auto-test/zhyy/library/CommonFun/handle_mq.py
qiaoxinjiu 6994b185a3 addproject
2026-01-22 19:10:37 +08:00

20 lines
611 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pika
# 设置RabbitMQ服务器的连接参数使用自定义端口9876
connection_params = pika.ConnectionParameters('rocketmq.qa.huohua.cn', 9876)
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()
# 声明一个队列,如果队列不存在则会创建
queue_name = 'my_queue'
channel.queue_declare(queue=queue_name)
# 发布消息到指定的队列
message = 'Hello, World!'
channel.basic_publish(exchange='',
routing_key=queue_name,
body=message)
print(f" [x] Sent '{message}'")
# 关闭连接
connection.close()