1 Introduction
Exchange: Message switch that specifies what the message according to the rules, which are routed to a queue.
Queue: vector message queue, each message will be put into one or more queues.
Binding: binding, its role is to exchange routing and queue bind in accordance with the rules.
Routing Key: routing keywords, exchange of messages delivered in accordance with this keyword.
vhost: web hosting, where a broker can offer more vhost, as a different user privilege separation.
producer: news producer, is the program delivered the message.
consumer: consumer news, is that the program accepts messages.
channel: message channels, each client's connection can be established multiple channel, each channel representing a session task.
(2) a declaration client exchange, and provision of the relevant property.
(3) The client declares a queue, and set the associated property.
(4) The client uses routing key, establish a good relationship between the exchange and the binding queue.
(5) the delivery message to the client exchange.
pip install pika
3. Sample Test
Examples of message content is transmitted from the send.py to rabbitmq, receive.py send.py receiving information transmitted from rabbitmq.
P represents Produce, meaning producers, may also be referred to as the sender, examples showed send.py;
C represents the consumer, the consumer's meaning, also known as the receiver, examples showed receive.py;
Red indicates that the queue intermediate meaning example showed hello queue.
send.py
!. 1 # / usr / bin / the env to python3 2 # - * - Coding: UTF-. 8 - * - . 3 . 4 Import the PIKA . 5 Import Random . 6 . 7 # new connection, rabbitmq installed locally, hostname is' localhost ' . 8 hostname =' 192.168.1.133 ' . 9 pika.ConnectionParameters Parameters = (hostname) 10 = Connection pika.BlockingConnection (Parameters) . 11 12 is to create a channel # 13 is connection.channel channel = () 14 # declare a queue, a producer and consumer must declare the same queue, is used to prevent the case linked to a party, the other can operate normally 15 channel.queue_declare (queue = 'Hello') 16 . 17 the random.randint Number = (. 1, 1000) 18 is body = 'Hello World:% S '% Number . 19 # switch; queue name, indicate the queue to which the message is sent; message content 20 # routing_key only needs to be specified when using anonymous switch, which indicates to the transmission queue 21 channel.basic_publish (exchange =' ', routing_key =' hello ',body=body) 22 print " [x] Sent %s" % body 23 connection.close()
receive.py
!. 1 # / usr / bin / the env to python3 2 # - * - Coding: UTF-. 8 - * - . 3 . 4 Import the PIKA . 5 . 6 hostname = '192.168.1.133' . 7 pika.ConnectionParameters Parameters = (hostname) . 8 = Connection the PIKA. the BlockingConnection (Parameters) . 9 10 create channels # . 11 = channel connection.channel () 12 is channel.queue_declare (Queue = 'Hello') 13 is 14 15 the callback DEF (CH, Method, Properties, body): 16 Print "[X] Received R & lt% "% (body,) . 17 18 is used rabbitmq # tell callback to receive information . 19 channel.basic_consume (callback, queue = 'Hello', NO_ACK = True) 20 is 21 is the start # receiving information, and enters the blocking state, there queue information will be called callback processing, exit press ctrl + c 22 print ' [*] Waiting for messages. To exit press CTRL+C' 23 channel.start_consuming()
Let's run send.py send a message:
Run receive.py received message:
1 Introduction
Exchange: Message switch that specifies what the message according to the rules, which are routed to a queue.
Queue: vector message queue, each message will be put into one or more queues.
Binding: binding, its role is to exchange routing and queue bind in accordance with the rules.
Routing Key: routing keywords, exchange of messages delivered in accordance with this keyword.
vhost: web hosting, where a broker can offer more vhost, as a different user privilege separation.
producer: news producer, is the program delivered the message.
consumer: consumer news, is that the program accepts messages.
channel: message channels, each client's connection can be established multiple channel, each channel representing a session task.
(2) a declaration client exchange, and provision of the relevant property.
(3) The client declares a queue, and set the associated property.
(4) The client uses routing key, establish a good relationship between the exchange and the binding queue.
(5) the delivery message to the client exchange.
pip install pika
3. Sample Test
Examples of message content is transmitted from the send.py to rabbitmq, receive.py send.py receiving information transmitted from rabbitmq.
P represents Produce, meaning producers, may also be referred to as the sender, examples showed send.py;
C represents the consumer, the consumer's meaning, also known as the receiver, examples showed receive.py;
Red indicates that the queue intermediate meaning example showed hello queue.
send.py
!. 1 # / usr / bin / the env to python3 2 # - * - Coding: UTF-. 8 - * - . 3 . 4 Import the PIKA . 5 Import Random . 6 . 7 # new connection, rabbitmq installed locally, hostname is' localhost ' . 8 hostname =' 192.168.1.133 ' . 9 pika.ConnectionParameters Parameters = (hostname) 10 = Connection pika.BlockingConnection (Parameters) . 11 12 is to create a channel # 13 is connection.channel channel = () 14 # declare a queue, a producer and consumer must declare the same queue, is used to prevent the case linked to a party, the other can operate normally 15 channel.queue_declare (queue = 'Hello') 16 . 17 the random.randint Number = (. 1, 1000) 18 is body = 'Hello World:% S '% Number . 19 # switch; queue name, indicate the queue to which the message is sent; message content 20 # routing_key only needs to be specified when using anonymous switch, which indicates to the transmission queue 21 channel.basic_publish(exchange='', routing_key='hello', body=body) 22 print " [x] Sent %s" % body 23 connection.close()
receive.py
!. 1 # / usr / bin / the env to python3 2 # - * - Coding: UTF-. 8 - * - . 3 . 4 Import the PIKA . 5 . 6 hostname = '192.168.1.133' . 7 pika.ConnectionParameters Parameters = (hostname) . 8 = Connection the PIKA. the BlockingConnection (Parameters) . 9 10 create channels # . 11 = channel connection.channel () 12 is channel.queue_declare (Queue = 'Hello') 13 is 14 15 the callback DEF (CH, Method, Properties, body): 16 Print "[X] Received R & lt% "% (body,) . 17 18 is used rabbitmq # tell callback to receive information . 19 channel.basic_consume (callback, queue = 'Hello', NO_ACK = True) 20 is 21 is the start # receiving information, and enters the blocking state, there queue information will be called callback processing, exit press ctrl + c 22 print ' [*] Waiting for messages. To exit press CTRL+C' 23 channel.start_consuming()
Let's run send.py send a message:
Run receive.py received message: