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
連接參數
連接參數主要是在是使用ConnectionParameters和URLParameters
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的類, 這個將映射對應的默認值到對應的參數
參數說明:
-
host
DEFAULT_HOST = ‘localhost’
-
port
DEFAULT_PORT = 5672
-
virtual_host
DEFAULT_VIRTUAL_HOST = ‘/’
-
credentials
認證參數:
默認值:DEFAULT_CREDENTIALS = pika.credentials.PlainCredentials(DEFAULT_USERNAME, DEFAULT_PASSWORD)
DEFAULT_USERNAME = ‘guest’
DEFAULT_PASSWORD = ‘guest’ -
channel_max
最大通道數
DEFAULT_CHANNEL_MAX = pika.channel.MAX_CHANNELS
-
frame_max
要使用的所需最大AMQP幀大小
DEFAULT_FRAME_MAX = spec.FRAME_MAX_SIZE
-
heartbeat
心跳, 0 為關閉。連接調整期間協商的AMQP連接心跳超時值或連接調整期間調用的可調用值
DEFAULT_HEARTBEAT_TIMEOUT = None # None accepts server’s proposal
-
ssl_options
傳入值pika.SSLOptions
DEFAULT_SSL_OPTIONS = None
-
connection_attempts
套接字連接嘗試次數
DEFAULT_CONNECTION_ATTEMPTS = 1
-
retry_delay
套接字連接嘗試重連間隔
DEFAULT_RETRY_DELAY = 2.0
-
socket_timeout
DEFAULT_SOCKET_TIMEOUT = 10.0 # socket.connect() timeout
-
stack_timeout
套接字連接嘗試間隔 , None為禁用
DEFAULT_STACK_TIMEOUT = 15.0 # full-stack TCP/[SSl]/AMQP bring-up timeout
-
locale
DEFAULT_LOCALE = ‘en_US’
-
blocked_connection_timeout
阻塞的超時時間,默認不超時
DEFAULT_BLOCKED_CONNECTION_TIMEOUT = None
-
client_properties
客戶端屬性,用於覆蓋通過Connection.StartOk 方法向RabbitMQ報告的默認客戶端屬性中的字段,
字典類型/None
DEFAULT_CLIENT_PROPERTIES = None
-
tcp_options
DEFAULT_TCP_OPTIONS = None
其他:
DEFAULT_SSL = False
DEFAULT_SSL_PORT = 5671
URLParameters
這里不做詳細介紹,具體可參考官方
例如:
parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')