python pika rabbitmq demo

import pika
import json

# https://www.cnblogs.com/zhaohuaxishi/p/12107392.html
# https://www.cnblogs.com/shenh/p/10497244.html

credentials = pika.PlainCredentials('用户名', '密码')  # mq用户名和密码
# 虚拟队列需要指定参数 virtual_host,如果是默认的可以不填。
connection = pika.BlockingConnection(pika.ConnectionParameters(host = 'IP地址',port = 5672,virtual_host = '/',credentials = credentials))
channel=connection.channel()
# 声明消息队列,消息将在这个队列传递,如不存在,则创建
result = channel.queue_declare(queue = 'pay.test.python')

#for i in range(10):
#message=json.dumps({'OrderId':"1000%s"%i})
# 向队列插入数值 routing_key是队列名
message="消息体的json串"
channel.basic_publish(exchange = 'test-message',routing_key = 'message.success.test',body = message)
print(message)
connection.close()

 

posted on 2020-11-20 08:34  oktokeep  阅读(162)  评论(0编辑  收藏  举报