pika详解(五)登录认证及connectionParameters

pika详解(五)登录认证及connectionParameters

pika
登录认证

使用Pika进行身份验证,需要创建一个PlainCredentials 传递用户名和密码的对象,并将其作为凭证参数值传递给ConnectionParameters

class pika.credentials.PlainCredentials(username, password,erase_on_connect =False)
 

erase_on_connect 在连接后清除用户名密码,PlainCredentials 中是以明文记录用户名密码的, 默认是不清除

异常类:

exception pika.exceptions.AMQPChannelError
exception pika.exceptions.AMQPConnectionError
exception pika.exceptions.AMQPError
exception pika.exceptions.AMQPHeartbeatTimeout
exception pika.exceptions.AuthenticationError
exception pika.exceptions.BodyTooLongError
exception pika.exceptions.ChannelClosed(reply_code, reply_text)
exception pika.exceptions.ChannelClosedByBroker(reply_code, reply_text)
exception pika.exceptions.ChannelClosedByClient(reply_code, reply_text)
exception pika.exceptions.ChannelError
exception pika.exceptions.ChannelWrongStateError
exception pika.exceptions.ConnectionBlockedTimeout
exception pika.exceptions.ConnectionClosedByBroker(reply_code, reply_text)
exception pika.exceptions.ConnectionClosedByClient(reply_code, reply_text)
exception pika.exceptions.ConnectionOpenAborted
exception pika.exceptions.ConnectionWrongStateError
exception pika.exceptions.ConsumerCancelled
exception pika.exceptions.DuplicateConsumerTag
exception pika.exceptions.DuplicateGetOkCallback
exception pika.exceptions.IncompatibleProtocolError
exception pika.exceptions.InvalidChannelNumber
exception pika.exceptions.InvalidFieldTypeException
exception pika.exceptions.InvalidFrameError
exception pika.exceptions.MethodNotImplemented
exception pika.exceptions.NackError(messages)
exception pika.exceptions.NoFreeChannels
exception pika.exceptions.ProbableAccessDeniedError
exception pika.exceptions.ProbableAuthenticationError
exception pika.exceptions.ProtocolSyntaxError
exception pika.exceptions.ProtocolVersionMismatch
exception pika.exceptions.ReentrancyError
exception pika.exceptions.ShortStringTooLong
exception pika.exceptions.StreamLostError
exception pika.exceptions.UnexpectedFrameError
exception pika.exceptions.UnroutableError(messages)
exception pika.exceptions.UnsupportedAMQPFieldException
 

 

连接参数

连接参数主要是在是使用ConnectionParametersURLParameters

connectionParameters定义简化为:

class ConnectionParameters(Parameters):
    def __init__(self,
            host=_DEFAULT,
            port=_DEFAULT,
            virtual_host=_DEFAULT,
            credentials=_DEFAULT,
            channel_max=_DEFAULT,
            frame_max=_DEFAULT,
            heartbeat=_DEFAULT,
            ssl_options=_DEFAULT,
            connection_attempts=_DEFAULT,
            retry_delay=_DEFAULT,
            socket_timeout=_DEFAULT,
            stack_timeout=_DEFAULT,
            locale=_DEFAULT,
            blocked_connection_timeout=_DEFAULT,
            client_properties=_DEFAULT,
            tcp_options=_DEFAULT,
            **kwargs)
 

 

参数默认值都是一个_DEFAULT的类, 这个将映射对应的默认值到对应的参数

参数说明:

  1. host

    DEFAULT_HOST = ‘localhost’

  2. port

    DEFAULT_PORT = 5672

  3. virtual_host

    DEFAULT_VIRTUAL_HOST = ‘/’

  4. credentials

    认证参数:

    默认值:DEFAULT_CREDENTIALS = pika.credentials.PlainCredentials(DEFAULT_USERNAME, DEFAULT_PASSWORD)

    DEFAULT_USERNAME = ‘guest’
    DEFAULT_PASSWORD = ‘guest’

  5. channel_max

    最大通道数

    DEFAULT_CHANNEL_MAX = pika.channel.MAX_CHANNELS

  6. frame_max

    要使用的所需最大AMQP帧大小

    DEFAULT_FRAME_MAX = spec.FRAME_MAX_SIZE

  7. heartbeat

    心跳, 0 为关闭。连接调整期间协商的AMQP连接心跳超时值或连接调整期间调用的可调用值

    DEFAULT_HEARTBEAT_TIMEOUT = None # None accepts server’s proposal

  8. ssl_options

    传入值pika.SSLOptions

    DEFAULT_SSL_OPTIONS = None

  9. connection_attempts

    套接字连接尝试次数

    DEFAULT_CONNECTION_ATTEMPTS = 1

  10. retry_delay

    套接字连接尝试重连间隔

    DEFAULT_RETRY_DELAY = 2.0

  11. socket_timeout

    DEFAULT_SOCKET_TIMEOUT = 10.0 # socket.connect() timeout

  12. stack_timeout

    套接字连接尝试间隔 , None为禁用

    DEFAULT_STACK_TIMEOUT = 15.0 # full-stack TCP/[SSl]/AMQP bring-up timeout

  13. locale

    DEFAULT_LOCALE = ‘en_US’

  14. blocked_connection_timeout

    阻塞的超时时间,默认不超时

    DEFAULT_BLOCKED_CONNECTION_TIMEOUT = None

  15. client_properties

    客户端属性,用于覆盖通过Connection.StartOk 方法向RabbitMQ报告的默认客户端属性中的字段,

    字典类型/None

    DEFAULT_CLIENT_PROPERTIES = None

  16. tcp_options

    DEFAULT_TCP_OPTIONS = None

其他:

DEFAULT_SSL = False
DEFAULT_SSL_PORT = 5671

URLParameters

这里不做详细介绍,具体可参考官方

例如:

parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')
 
原文地址:https://www.cnblogs.com/xiao-xue-di/p/13856728.html