rabbitmq官方代码: https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/python/send.py

自己更改后的版本:

send.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pika
import time


def push_mq(body_mq):
    # 权限认证
    credentials = pika.PlainCredentials('admin', 'admin')

    # 建立连接
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host='192.168.2.226', virtual_host='/', credentials=credentials))

    # 创建信道
    channel = connection.channel()

    # # 声明交换器和队列,durable参数指定持久化,默认是false, 如已创建,则可省略。
    # channel.exchange_declare(exchange="langtest", exchange_type="direct", durable=True, auto_delete=False)
    # channel.queue_declare(queue='hello', durable=True)
    #
    # # 绑定
    # channel.queue_bind(queue='hello', exchange='langtest', routing_key='test')

    # 发布消息, delivery_mode=2设置消息为持久化
    channel.basic_publish(exchange='langtest', routing_key='test', body=bytes(str(body_mq), encoding='utf-8'),
                          properties=pika.BasicProperties(delivery_mode=2))

    print(body)
    channel.close()
    connection.close()


for body in range(1, 100000000000000):
    push_mq(body_mq=body)
    time.sleep(0.01)

receive.py基本没做改动。