Connection Parameters

To maintain flexibility in how you specify the connection information required for your applications to properly connect to RabbitMQ, pika implements two classes for encapsulating the information, ConnectionParameters and URLParameters.

ConnectionParameters

The classic object for specifying all of the connection parameters required to connect to RabbitMQ, ConnectionParameters provides attributes for tweaking every possible connection option.

Example:

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('guest', 'guest')
parameters = pika.ConnectionParameters('rabbit-server1',
                                       5672,
                                       '/',
                                       credentials)
class pika.connection.ConnectionParameters(host=<class 'pika.connection._DEFAULT'>, port=<class 'pika.connection._DEFAULT'>, virtual_host=<class 'pika.connection._DEFAULT'>, credentials=<class 'pika.connection._DEFAULT'>, channel_max=<class 'pika.connection._DEFAULT'>, frame_max=<class 'pika.connection._DEFAULT'>, heartbeat=<class 'pika.connection._DEFAULT'>, ssl_options=<class 'pika.connection._DEFAULT'>, connection_attempts=<class 'pika.connection._DEFAULT'>, retry_delay=<class 'pika.connection._DEFAULT'>, socket_timeout=<class 'pika.connection._DEFAULT'>, locale=<class 'pika.connection._DEFAULT'>, backpressure_detection=<class 'pika.connection._DEFAULT'>, blocked_connection_timeout=<class 'pika.connection._DEFAULT'>, client_properties=<class 'pika.connection._DEFAULT'>, tcp_options=<class 'pika.connection._DEFAULT'>, **kwargs)[source]

Connection parameters object that is passed into the connection adapter upon construction.

backpressure_detection
Returns:boolean indicating whether backpressure detection is enabled. Defaults to DEFAULT_BACKPRESSURE_DETECTION.
blocked_connection_timeout
Returns:None or float blocked connection timeout. Defaults to DEFAULT_BLOCKED_CONNECTION_TIMEOUT.
channel_max
Returns:max preferred number of channels. Defaults to DEFAULT_CHANNEL_MAX.
Return type:int
client_properties
Returns:None or dict of client properties used to override the fields in the default client poperties reported to RabbitMQ via Connection.StartOk method. Defaults to DEFAULT_CLIENT_PROPERTIES.
connection_attempts
Returns:number of socket connection attempts. Defaults to DEFAULT_CONNECTION_ATTEMPTS.
credentials
Return type:one of the classes from pika.credentials.VALID_TYPES. Defaults to DEFAULT_CREDENTIALS.
frame_max
Returns:desired maximum AMQP frame size to use. Defaults to DEFAULT_FRAME_MAX.
heartbeat
Returns:AMQP connection heartbeat timeout value for negotiation during connection tuning or callable which is invoked during connection tuning. None to accept broker’s value. 0 turns heartbeat off. Defaults to DEFAULT_HEARTBEAT_TIMEOUT.
Return type:integer, None or callable
host
Returns:hostname or ip address of broker. Defaults to DEFAULT_HOST.
Return type:str
locale
Returns:locale value to pass to broker; e.g., ‘en_US’. Defaults to DEFAULT_LOCALE.
Return type:str
retry_delay
Returns:interval between socket connection attempts; see also connection_attempts. Defaults to DEFAULT_RETRY_DELAY.
Return type:float
socket_timeout
Returns:socket timeout value. Defaults to DEFAULT_SOCKET_TIMEOUT.
Return type:float
ssl_options
Returns:None for plaintext or pika.SSLOptions instance for SSL/TLS.
Return type:`pika.SSLOptions`|None
port
Returns:port number of broker’s listening socket. Defaults to DEFAULT_PORT.
Return type:int
virtual_host
Returns:rabbitmq virtual host name. Defaults to DEFAULT_VIRTUAL_HOST.
tcp_options
Returns:None or a dict of options to pass to the underlying socket
Return type:dict|None

URLParameters

The URLParameters class allows you to pass in an AMQP URL when creating the object and supports the host, port, virtual host, ssl, username and password in the base URL and other options are passed in via query parameters.

Example:

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')
class pika.connection.URLParameters(url)[source]

Connect to RabbitMQ via an AMQP URL in the format:

amqp://username:password@host:port/<virtual_host>[?query-string]

Ensure that the virtual host is URI encoded when specified. For example if you are using the default “/” virtual host, the value should be %2f.

See Parameters for default values.

Valid query string values are:

  • backpressure_detection:
    DEPRECATED in favor of Connection.Blocked and Connection.Unblocked. See Connection.add_on_connection_blocked_callback.
  • channel_max:
    Override the default maximum channel count value
  • client_properties:
    dict of client properties used to override the fields in the default client properties reported to RabbitMQ via Connection.StartOk method
  • connection_attempts:
    Specify how many times pika should try and reconnect before it gives up
  • frame_max:
    Override the default maximum frame size for communication
  • heartbeat:
    Desired connection heartbeat timeout for negotiation. If not present the broker’s value is accepted. 0 turns heartbeat off.
  • locale:
    Override the default en_US locale value
  • ssl_options:
    None for plaintext; for SSL: dict of public ssl context-related arguments that may be passed to ssl.SSLSocket() as kwargs, except sock, server_side,`do_handshake_on_connect`, family, type, proto, fileno.
  • retry_delay:
    The number of seconds to sleep before attempting to connect on connection failure.
  • socket_timeout:
    Override low level socket timeout value
  • blocked_connection_timeout:
    Set the timeout, in seconds, that the connection may remain blocked (triggered by Connection.Blocked from broker); if the timeout expires before connection becomes unblocked, the connection will be torn down, triggering the connection’s on_close_callback
  • tcp_options:
    Set the tcp options for the underlying socket.
Parameters:url (str) – The AMQP URL to connect to
ssl_options
Returns:None for plaintext or pika.SSLOptions instance for SSL/TLS.
Return type:`pika.SSLOptions`|None
host
Returns:hostname or ip address of broker. Defaults to DEFAULT_HOST.
Return type:str
port
Returns:port number of broker’s listening socket. Defaults to DEFAULT_PORT.
Return type:int
credentials
Return type:one of the classes from pika.credentials.VALID_TYPES. Defaults to DEFAULT_CREDENTIALS.
virtual_host
Returns:rabbitmq virtual host name. Defaults to DEFAULT_VIRTUAL_HOST.
backpressure_detection
Returns:boolean indicating whether backpressure detection is enabled. Defaults to DEFAULT_BACKPRESSURE_DETECTION.
blocked_connection_timeout
Returns:None or float blocked connection timeout. Defaults to DEFAULT_BLOCKED_CONNECTION_TIMEOUT.
channel_max
Returns:max preferred number of channels. Defaults to DEFAULT_CHANNEL_MAX.
Return type:int
client_properties
Returns:None or dict of client properties used to override the fields in the default client poperties reported to RabbitMQ via Connection.StartOk method. Defaults to DEFAULT_CLIENT_PROPERTIES.
connection_attempts
Returns:number of socket connection attempts. Defaults to DEFAULT_CONNECTION_ATTEMPTS.
frame_max
Returns:desired maximum AMQP frame size to use. Defaults to DEFAULT_FRAME_MAX.
heartbeat
Returns:AMQP connection heartbeat timeout value for negotiation during connection tuning or callable which is invoked during connection tuning. None to accept broker’s value. 0 turns heartbeat off. Defaults to DEFAULT_HEARTBEAT_TIMEOUT.
Return type:integer, None or callable
locale
Returns:locale value to pass to broker; e.g., ‘en_US’. Defaults to DEFAULT_LOCALE.
Return type:str
retry_delay
Returns:interval between socket connection attempts; see also connection_attempts. Defaults to DEFAULT_RETRY_DELAY.
Return type:float
socket_timeout
Returns:socket timeout value. Defaults to DEFAULT_SOCKET_TIMEOUT.
Return type:float
tcp_options
Returns:None or a dict of options to pass to the underlying socket
Return type:dict|None