知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议进行许可。

接收端
#coding=utf-8
__author__ = 'nate'

import pika
from pika.exceptions import ProbableAccessDeniedError
from pika.exceptions import ProbableAuthenticationError


def get(ch, method, props, body):
    print 'message: %r' % body


if __name__ == '__main__':
    parameters = pika.URLParameters('amqp://nate:123123@10.211.55.61:5672/test_vhost')
    conn = None
    channel = None
    try:
        conn = pika.BlockingConnection(parameters)
        channel = conn.channel()

        channel.exchange_declare(exchange='logs',
                                 type='direct')
        result = channel.queue_declare(exclusive=True)
        queue_name = result.method.queue

        channel.queue_bind(exchange='logs',
                           queue=queue_name,
                           routing_key='test')
        channel.basic_consume(get,
                              queue=queue_name,
                              no_ack=True)
        channel.start_consuming()
    except ProbableAccessDeniedError:
        print 'Access Error'
    except ProbableAuthenticationError:
        print 'Auth Error'
    finally:
        if channel:
            channel.close()
        if conn:
            conn.close()
发送端
#coding=utf-8
__author__ = 'nate'

import pika
from pika.exceptions import ProbableAccessDeniedError
from pika.exceptions import ProbableAuthenticationError

if __name__ == '__main__':
    parameters = pika.URLParameters('amqp://nate:123123@10.211.55.61:5672/test_vhost')
    conn = None
    channel = None
    try:
        conn = pika.BlockingConnection(parameters)
        channel = conn.channel()
        channel.exchange_declare(exchange='logs',
                                 type='direct')

        channel.basic_publish(exchange='logs',
                              routing_key='test',
                              body='This is Test!')
    except ProbableAccessDeniedError:
        print 'Access Error'
    except ProbableAuthenticationError:
        print 'Auth Error'
    finally:
        if channel:
            channel.close()
        if conn:
            conn.close()