RABBITMQ installation and use of Python connection rabbitmq

Source: Internet
Author: User
Tags rabbitmq

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

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.