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

原文:https://blog.csdn.net/comprel/article/details/94662916

pika
登录认证

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

class pika.credentials.PlainCredentials(username, password,erase_on_connect =False)
1
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
连接参数

连接参数主要是在是使用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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
参数默认值都是一个_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
1
2
URLParameters

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

例如:

parameters = pika.URLParameters('amqp://guest:guest@rabbit-server1:5672/%2F')

 
posted @ 2021-10-31 15:00  liujiacai  阅读(655)  评论(0编辑  收藏  举报