RabbitMQ兩種認證方式


pika提供了兩種認證方式:ConnectinParameters和URLParameters。

  1.ConnectionParameters:

import pika

# Set the connection parameters to connect to rabbit-server1 on port 5672# on the / virtual host using the username "guest" and password "guest"
credentials = pika.PlainCredentials('root', 'root')
parameters = pika.ConnectionParameters('rabbit-server1',
                                       5672,
                                       '/',
                                       credentials)

  2.URLParameters:

import pika

# Set the connection parameters to connect to rabbit-server1 on port 5672# on the / virtual host using the username "guest" and password "guest"
parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')

  3.例子

import pika

i = 1

def callback(ch, method, properties, body):
    global i
    #print 'receive %r'%body
    print 'receive %s'%i
    i += 1
    f = open('%s'%i, 'w+')
    f.write(body)
    f.close()

#第一種方法
#credentials = pika.PlainCredentials('mtest', 'root')
#connection = pika.BlockingConnection(pika.ConnectionParameters('rabbit-server', 5672, '/', credentials))
#第二種方法
parameters = pika.URLParameters('amqp://mtest:root@rabbit-server:5672/%2F')
connection = pika.BlockingConnection(parameters)

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_consume(callback, queue='hello1', no_ack=True)

channel.start_consuming()     

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM