#! /usr/bin/python # -*- coding: utf-8 -*- import amqplib.client_0_8 as amqp # 创建一个TCP 连接 conn = amqp.Connection(host="localhost:5672 ", userid="guest", password="guest", virtual_host="/", insist=False) # 创建一个通信channel chan = conn.channel() # 创建一个队列 chan.queue_declare(queue="po_box", durable=True, exclusive=False, auto_delete=False) # 创建一个交换机 chan.exchange_declare(exchange="sorting_room", type="direct", durable=True, auto_delete=False) # 绑定 chan.queue_bind(queue="po_box", exchange="sorting_room",routing_key="jason") def recv_callback(msg): print 'Received: ' + msg.body + ' from channel #' + str(msg.channel.channel_id) chan.basic_consume(queue='po_box', no_ack=True, callback=recv_callback, consumer_tag="testtag") while True: chan.wait() # 该句不会执行 chan.basic_cancel("testtag") chan.close() conn.close()