Introduction
The environment here uses the Mac OS X system, all configured and used based on Mac OS X and Python 2.7 and the corresponding Pika library.
RABBITMQ Installation and Configuration installation section
#brew install rabbitmq
Configure and start
#sudo brew services start rabbitmq#sudo rabbitmqctl add_user admin admin "创建用户(username password)"#sudo rabbitmqctl set_user_tags admin administrator "配置用户为超级管理员"#sudo rabbitmqctl set_permissions -p ‘/‘ admin ‘.‘ ‘.‘ ‘.‘ " 配置权限"#sudo brew services restart rabbitmq
Python and Pika Installation Pika
#sudo pip install pika
Python's collaboration with RABBITMQ (Basic)
#-*-Coding:utf-8-*-ImportPikaclassRabbitMQ (Object):"" " a simple library of RABBITMQ, in order to learn" "" def __init__( Self, Host,port=5672, username=None, password=None):ifHost== None:Raise Exception("Host is None")ifUsername!= None andPassword!= None:Try: Self. Credentials=Pika. Plaincredentials (Username,password) Self. Connectionkeys=Pika. Connectionparameters (Host,port,'/', Self. Credentials)except Exception:Raise Else: Self. Connectionkeys= None def_connection ( Self):Try:if Self. Connectionkeys== None: Self. Connection=Pika. Blockingconnection ()Else: Self. Connection=Pika. Blockingconnection ( Self. Connectionkeys)except Exception:Raise def_channel ( Self): Self. Channel= Self. Connection.channel ()def_callback (channel,method,properties,body):returnBodydef Connect( Self): Self. _connection () Self. _channel ()defCreate_queque (Slef,flag,durableflag=False):Try: Self. Channel.queue_declare (Queue=Flag,durable=Durableflag)except Exception:Raise defProduct Self, Flag,content,exchange="'):Try: Self. Channel.basic_publish (Exchange="', Routing_key=Flag,body=Contentexcept Exception:Raise defConsume (Slef,flag,ackflag):Try: Self. Channel.basic_consume ( Self. _callback,queue=Flag,no_ack=Ackflag)except Exception:Raise defClose Self):Try: Self. Connection.close ()except Exception:Raise
RABBITMQ installation and use of Python connection rabbitmq