Python uses RABBITMQ for simple message forwarding

Source: Internet
Author: User
Tags rabbitmq

Get ready:

1. Download the support environment for the Elang language http://www.erlang.org/download.html (RABBITMQ developed using it)

2. Download the RABBITMQ software http://www.rabbitmq.com/releases/rabbitmq-server/(depending on your system selection)

3. Install ELANG,RABBITMQ, and configure the environment variables, my is windows7, the steps are as follows:

A. New variable: Erlang_home=d:\program files\erl6.1, then add%erlang_home%\bin in Path;

B. New variable: Rabbitmq_server=d:\program files\rabbitmq_server-3.3.4, then add%rabbitmq_server%\sbin in Path;

C. Run Sbin/rabbitmq-server.bat, start the RABBITMQ server effect as

      

Working with Python RABBITMQ

1. Download the Pika Support Package (pip install pika)

2. Message delivery received by the producer consumer model

Generated by party code productor.py

1# __author__ ='STEVEN'2 Import Pika3 def b ():4 #建立socket5Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))6 #声明一个管道7Piple =Connection.channel ()8 #创建一个队列9Piple.queue_declare ('Hello')Ten #发送消息 OnePiple.basic_publish (exchange="', routing_key='Hello', body='Oh, hello.') APrint'[x] Sent message: Hello') - connection.close () -B ()

Consumer Square Code consumer.py

1 #__author__ = ' STEVEN '2 ImportPika3 #Create a socket link4Connection = Pika. Blockingconnection (Pika. Connectionparameters ('localhost'))5 #Creating Pipelines6Channel =Connection.channel ()7 #Create a queue8Channel.queue_declare ('Hello')9 #declaring a callback functionTen defCallback (ch,method,properties,body): One     Print('[x] recieved message%r'%Body.decode ()) A #Call the callback function if the message is accepted, ready to accept the message -Channel.basic_consume (callback,queue='Hello', no_ack=True) - Print('[*] is waiting for recieve mess press CTRL + C to Eixt') the #Start Consumer News -Channel.start_consuming ()

Python uses RABBITMQ for simple message forwarding

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.