Send side:
Import Pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' localhost ')) channel = Connection.channel () channel.exchange_declare (exchange= ' logs ', Exchange_type= ' fanout ') message = '. Join (sys.argv[1:]) or "Info:hello world!" Channel.basic_publish (exchange= ' logs ', routing_key= ', body=message) print ("[x] Sent%r "% message) Connection.close ()
Receiving end:
Import pikaconnection = pika. Blockingconnection (Pika. Connectionparameters ( host= ' localhost ')) Channel = connection.channel () Channel.exchange_declare (exchange= ' logs ', exchange_type= ' Fanout ') Result = channel.queue_declare (exclusive=true) # do not specify a queue name, rabbit will randomly assign a name, Exclusive=true will automatically delete the queue after the consumer using this queue disconnects Queue_name = result.method.queueprint (queue_name) Channel.queue_bind (exchange= ' logs ', queue=queue_name) print (' [*] waiting for logs. to exit press ctrl+c ') Def callback (ch, method, properties, body): print (" [x] %r" % body) Channel.basic_consume (callback, queue=queue_name, no_ack=true) channel.start_consuming ()
Python RABBITMQ Message Publishing subscription