zzck/mqsend.py

26 lines
801 B
Python
Raw Permalink Normal View History

2025-09-02 15:15:51 +08:00
import pika
from config import *
def send_message(message):
# 连接 RabbitMQ
credentials = pika.PlainCredentials(mq_user, mq_password)
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost', credentials=credentials)
)
channel = connection.channel()
channel.exchange_declare(exchange='zzck_exchange', exchange_type='fanout')
# 声明队列
# channel.queue_declare(queue=mq_queue)
# 发送消息
channel.basic_publish( exchange='zzck_exchange',
routing_key='',
body=message,
properties=pika.BasicProperties(
expiration='10' # 消息1秒后过期
)
)
connection.close()