说明
* 表示匹配一个词
# 表示一个或多个词
接收端
#codig=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:[email protected]:5672/test_vhost')
conn = None
channel = None
try:
conn = pika.BlockingConnection(parameters)
channel = conn.channel()
channel.exchange_declare(exchange='t_logs',
type='topic')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
#routing_key = '#'
#routing_key = 'log.*'
routing_key = 'log.#'
channel.queue_bind(exchange='t_logs',
queue=queue_name,
routing_key=routing_key)
channel.basic_consume(get,
queue=queue_name,
no_ack=True)
channel.start_consuming()
except ProbableAccessDeniedError:
print 'Access Error'
except ProbableAuthenticationError:
print 'Auth Error'
except KeyboardInterrupt:
print 'Exit'
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:[email protected]:5672/test_vhost')
conn = None
channel = None
try:
conn = pika.BlockingConnection(parameters)
channel = conn.channel()
channel.exchange_declare(exchange='t_logs',
type='topic')
routing_key = 'log.info'
channel.basic_publish(exchange='t_logs',
routing_key=routing_key,
body='This is Test')
except ProbableAccessDeniedError:
print 'Access Error'
except ProbableAuthenticationError:
print 'Auth Error'
except KeyboardInterrupt:
print 'Exit'
finally:
if channel:
channel.close()
if conn:
conn.close()