Try rabbitmq in Python

Source: Internet
Author: User
Tags rabbitmq

Test Environment 1: win7

Test environment 2: ubuntu10.04

 

Install the Erlang Environment

1. Download: http://erlang.org/download.html

Download the source code package or win Binary Package as needed.

2. installation:

In Windows, there is nothing to say about it. All the way to next ~

In the Linux environment, I have installed Erlang on ubuntu10.04 and the version is rb13b03, but it does not seem to support the latest version of rabbitmq.

If you want to use the latest version of rabbitmq or have not installed the Erlang environment, you need to reinstall the latest version of Erlang.

Currently, the latest official version of Erlang is r14a,
First install some dependent packages

 

Sudo apt-Get install build-essential

Sudo apt-Get install libncurses5-dev

Sudo apt-Get install M4

Sudo apt-Get install libssl-Dev

Sudo apt-Get install libc6

Sudo apt-Get install unixodbc

Sudo apt-Get install unixodbc-devel

Sudo apt-Get install gcj ----------------- "dozens of megabytes, mainly used in Java, is useless to me for the moment, so I did not install it: P

Officially started installation

./Configure -- without-javac ---------- because I have not installed gcj, The javac option is disabled in configure.

Make

Sudo make install

If there is no accident, Erlang will be installed. You can check it with whereis Erlang.

 

Install rabbitmq

Ubuntu users can use apt-Get for automation.

Sudo apt-Get install rabbitmq-Server

After the installation is complete, the service will be automatically started. Everything is as simple as that: the general method is described below.

 

1. Download: http://www.rabbitmq.com/server.html

For normal Linux users, we recommend that you download the common source packaged for generic UNIX systems.

Windows users can download the packaged for Windows systems packaged directly.

2. Installation (Linux & windows ):

In fact, rabbitmq is now a green software without installation.

We recommend that you directly copy the entire directory to the lib directory of Erlang after decompression.

3. Run (Linux & windows ):

In rabbitmq, The sbin directory contains related tool scripts.

Run the script (not Daemon)

Sudo rabbitmq_server-1.8.1/sbin/rabbitmq-Server

[Windows] rabbitmq_server-1.8.1/sbin/rabbitmq-server.bat

For Windows users:

1. Set erlang_home as the root directory of Erlang ~)

2, if you need to run the service mode can use rabbitmq-service.bat installation service (win7 users pay attention to permission issues)

 

 

Install Py-amqplib

As mentioned in the Article [rabbitmq + Python getting started classic] Rabbit and rabbit nest

Based on your needs, both Py-amqplib and txamqp are supported. Because it is based on twisted, txamqp can ensure that ultra-high performance amqp programs are built using asynchronous Io. But twisted programming itself is a big topic ...... For clarity, we plan to use Py-amqplib.

1. Download: http://barryp.org/software/py-amqplib/

2. installation:

Very easy. Unzip the package and go to the directory.

[Linux] sudo./setup. py install
[Windows] Python setup. py install

After installing the tool, you can import the tool in Python :)

 

Simple demo Test

 

Py-amqplib has a simple demo, In the amqplib-0.6/demo, we first use the two to test the message sending and receiving

 

Demo_receive.py ------------------- message consumer

Demo_send.py ---------------------- message producer

After rabbitmq is run, run./demo_receive.py to create a broker (switch and queue)

Start another terminal and run./demo_send.py "Hello, linvo"

Wow, it's amazing to display it on the first terminal ~~~

 

You can specify parameters such as host, userid, and password of rabbitmq during runtime. The default host is the local machine, and both userid and password are guest.

You can use the rabbitmqctl tool to delete, create users, and set permissions.

./Rabbitmqctl delete_user guest ------- Delete the default Guest user

./Rabbitmqctl add_user linvo 111 --------- add new user linvo, password 111

./Rabbitmqctl set_permissions-P/linvo ". *" ". *". * "-----------" sets the permission of the virtual host of linvo in/to the same as that of the previous guest.

PS: do not understand the specific permission regular rules for the moment ......

Try again.

 

./Demo_receive.py-u linvo-P 111

./Demo_send.py-u linvo-P 111 "Hello, linvo"

 

More functions of rabbitmqctl can be found at http://www.rabbitmq.com/rabbitmqctl.1.man.html, :p

 

DIY demo Test

I drew one myself: dreceive. py. #! /Usr/bin/ENV python <br/> # Coding = UTF-8 <br/> Import amqplib. client_0_8 as amqp <br/> def showmsg (MSG): <br/> Print MSG. body <br/> MSG. channel. basic_ack (MSG. delivery_tag) <br/> If MSG. body = 'quit': <br/> MSG. channel. basic_cancel (MSG. consumer_tag) <br/> def main (): <br/> Server = {'host': 'localhost', 'userid': 'linvo ', 'Password ': '200', 'ssl ': false} <br/> x_name = 'x1' <br/> q_name = 'q1' <br/> conn = amqp. connection (server ['host'], <br/> userid = server ['userid'], <br/> Password = server ['Password'], <br/> SSL = server ['ssl ']) <br/> CH = Conn. channel () <br/> CH. access_request ('/data', active = true, READ = true) <br/> CH. exchange_declare (exchange = x_name, type = 'fanout', durable = true, auto_delete = false) <br/> CH. queue_declare (queue = q_name, durable = true, exclusive = false, auto_delete = false) <br/> CH. queue_bind (queue = q_name, exchange = x_name) <br/> CH. basic_consume (q_name, callback = showmsg) <br/> while ch. callbacks: <br/> CH. wait () <br/> CH. close () <br/> Conn. close () <br/> If _ name _ = '_ main _': <br/> main ()

 

Send. py

#! /Usr/bin/ENV python <br/> # Coding = UTF-8 <br/> Import amqplib. client_0_8 as amqp <br/> def main (): <br/> Server = {'host': 'localhost', 'userid': 'linvo ', 'Password ': '20140901', 'ssl ': false} <br/> x_name = 'x1' </P> <p> conn = amqp. connection (server ['host'], <br/> userid = server ['userid'], <br/> Password = server ['Password'], <br/> SSL = server ['ssl ']) <br/> CH = Conn. channel () <br/> CH. access_request ('/data', active = true, write = true) <br/> CH. exchange_declare (exchange = x_name, type = 'fanout', durable = true, auto_delete = false) <br/> retry = true <br/> while retry: <br/> msg_body = raw_input ('>') <br/> MSG = amqp. message (msg_body, content_encoding = 'utf-8') <br/> MSG. properties ['delivery _ mode'] = 2 <br/> CH. basic_publish (MSG, x_name) </P> <p> If msg_body = 'quit': <br/> retry = false </P> <p> CH. close () <br/> Conn. close () <br/> If _ name _ = '_ main _': <br/> main ()

 

Send message

 

Receive messages

 

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.