"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 x64kombu:4.1.0
"View queue status"
# View queue status via browser Http://192.168.0.xx:15672/api/queues/vhosttest/queuetest # View queue status from the command line curl-u User:password/http/ 192.168.0.xx:15672/api/queues/vhosttest/queuetest | JQ # View Queue Length via command line (messages = Messages_ready + messages_unacknowledged) curl-s-u user:password http://192.168.0.xx:15672/ Api/queues/vhosttest/queuetest | \ JQ '. Messages '
"Send.py"
#encoding: utf-8#author: walker#date: 2018-03-09#summary: Sender/producer Import os, sys, timefrom kombu import connectiondef main (): With connection (' Amqp://test:[email protected]:5672/vhosttest ') as conn:with conn.channel () as channel: #producer = producer (channel) Producer = channel. Producer () while true:message = time.strftime ('%h:%m:%s ', time.localtime ()) Producer.publish (body=message,retry=true,exchange= ' exchangetest ', routing_key= ' rkeytest ') print (' send message: %s ' % message) while True: # Check queue to get back message Count Queue = channel.queue_declare (queue= ' queuetest ', passive=true) messagecount = queue.message_countprint (' messageCount: %d ' % messagecount) IF&NBSP;MESSAGECOUNT&NBSP;<&NBSP;100:BREAKTIME.SLeep (1) if __name__ == ' __main__ ': Main ()
"Recv.py"
#encoding: utf-8#author:walker#date:2018-03-09#summary: Receiver/Consumer import OS, sys, timefrom kombu import Connection, Queuef Rom kombu.mixins import consumermixinclass C (consumermixin):d ef __init__ (self, Connection, Queuenmae): self.connection = Connectionself.queues = [Queue (Queuenmae, Durable=false)]def get_consumers (self, Consumer, channel): return [Consumer (Self.queues, Callbacks=[self.on_message]),]# receives the callback function that handles the message def on_message (self, Body, message):p rint ("Received%s"% Body) Message.ack () def Main (): With Connection (' Amqp://test:[email protected]:5672/vhosttest ') as CONN:C (Conn, ' Queuetest '). Run () if __name__ = = ' __main__ ': Main ()
"Related reading"
Walker * * *
Python3 basic usage of RabbitMQ through Kombu connection