Install and start RABBITMQ on Windows
53156015
Installing the Python Pika Library
Pip Install Pika
Writing a Send message client.py
1 #Coding:utf82 3 ImportPika4 5Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))#Create a connection6Channel = Connection.channel ()#Create a channel7Channel.queue_declare (queue='Hello')#The name of the message queue is Hello8Channel.basic_publish (exchange="',9routing_key='Hello',Tenbody='Hello world!')#set Routing_key (name of message queue) and body (Sent content) One Print("[x] sent ' Hello world! '") AConnection.close ()#Close Connection
Writing a listener message queue server.py
1 #Coding:utf82 3 ImportPika4 5Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))#Create a connection6Channel = Connection.channel ()#Establish a channel7Channel.queue_declare (queue='Hello')#The consumer and the queue are bound together, and the producers and the queue are also Hello8 9 Ten defCallback (ch, method, properties, body):#callback function Get message body One Print("[x] Received%r"%body) A - - Channel.basic_consume (Callback, theQueue='Hello', -no_ack=True) - - Print('[*] waiting for messages. To exit Press CTRL + C') +Channel.start_consuming ()#Create a dead loop, listen for Message Queuing, and use CTRL + C to end the listener
Execution server.py can listen to Message Queuing and execute client.py to initiate a client to send messages to the message queue.
Python pika Simple implementation of RABBITMQ communication