"RabbitMQ Server"
# inside the vhosttest there are exchangetest and queuetest through rkeytest bindings broker:192.168.0.xxvirtual host:vhosttestExchange:exchangetest Queue:queuetest Routing Key:rkeytest
"Python Environment"
Os:windows 10python:3.6.3 x64pika:0.11.2
"View queue status through browser"
Http://192.168.0.xx:15672/api/queues/vhosttest/queuetest
"Send.py"
#encoding: utf-8#author: walker#date: 2018-01-31#summary: Sender/producer Import os, sys, timeimport pikadef main (): Credentials = pika. Plaincredentials ("Test", "test") Parameters = pika. Connectionparameters (host= "192.168.0.xx", virtual_host= ' vhosttest ', credentials=credentials) Connection = pika. Blockingconnection (parameters) # connectivity RabbitMQchannel = Connection.channel () # Create channel queue = Channel.queue_declare (queue= ' queuetest ') # declare or create a queue while true: # loop sends messages to the queue message = time.strftime ('%h:%m:%s ', time.localtime ()) channel.basic_ Publish (exchange= ' exchangetest ', routing_key= ' rkeytest ', body=message) print (' Send message: %s ' % message) while true: # Check the queue to get the message count back Queue = channel.queue_declare (queue= ' queuetest ', passive=true) messagecount = queue.method.message_countprint (' messagecount: %d ' % messagecount) If messagecount < 100:breaktime.sleep (1) # close connection connection.close () if __name__ == ' __main__ ': Main ()
"Recv.py"
#encoding: utf-8#author: walker#date: 2018-01-31#summary: Receiver/consumer Import os, sys, timeimport pika# receive callback function for processing messages def consumercallback (channel, method, Properties, body):p rint ("received %s" % body) Def main (): Credentials = pika. Plaincredentials ("Test", "test") Parameters = pika. Connectionparameters (host= "192.168.0.xx", virtual_host= ' vhosttest ', credentials=credentials) Connection = pika. Blockingconnection (parameters) # connectivity RabbitMQchannel = Connection.channel () # Create channel queue = Channel.queue_declare (queue= ' queuetest ') # declare or create a queue # no_ack=true Turn on automatic confirmation, or the message after consumption will remain in the queue # no_ack = no_manual_ack = auto_ack; do not manually answer, Turn on auto answer mode Channel.basic_consume (consumercallback, queue= ' queuetest ', no_ack=true) print (' wait message ... ') channel.start_consuming () if __name__ == ' __main_ _ ': Main ()
"Related reading"
Walker * * *
Python3 basic usage of RabbitMQ through Pika connection