amqpy.connection module

AMQP Connections

class amqpy.connection.Connection(amqpy.abstract_channel.AbstractChannel)[source]

Bases: amqpy.abstract_channel.AbstractChannel

The connection class provides methods for a client to establish a network connection to a server, and for both peers to operate the connection thereafter

connected

@property

Check if connection is connected

Returns:True if connected, else False
Return type:bool
server_capabilities

@property

Get server capabilities

These properties are set only after successfully connecting.

Returns:server capabilities
Return type:dict
sock

@property

Access underlying TCP socket

Returns:socket
Return type:socket.socket
channels = None

Map of {channel_id: Channel} for all active channels

Type:dict[int, Channel]
transport = None
Type:amqpy.transport.Transport
__init__(host='localhost', port=5672, ssl=None, connect_timeout=None, userid='guest', password='guest', login_method='AMQPLAIN', virtual_host='/', locale='en_US', channel_max=65535, frame_max=131072, heartbeat=0, client_properties=None, on_blocked=None, on_unblocked=None)[source]

Create a connection to the specified host

If you are using SSL, make sure the correct port number is specified (usually 5671), as the default of 5672 is for non-SSL connections.

Parameters:
  • host (str) – host
  • port (int) – port
  • ssl (dict or None) – dict of SSL options passed to ssl.wrap_socket(), None to disable SSL
  • connect_timeout (float or None) – connect timeout
  • userid (str) – username
  • password (str) – password
  • login_method (str) – login method (this is server-specific); default is for RabbitMQ
  • virtual_host (str) – virtual host
  • locale (str) – locale
  • channel_max (int) – maximum number of channels
  • frame_max (int) – maximum frame payload size in bytes
  • heartbeat (float) – heartbeat interval in seconds, 0 disables heartbeat
  • client_properties (dict or None) – dict of client properties
  • on_blocked (Callable or None) – callback on connection blocked
  • on_unblocked (Callable or None) – callback on connection unblocked
channel(channel_id=None) → amqpy.channel.Channel[source]

Create a new channel, or fetch the channel associated with channel_id if specified

Parameters:channel_id (int or None) – channel ID number
Returns:Channel
Return type:amqpy.channel.Channel
close(reply_code=0, reply_text='', method_type=method_t(class_id=0, method_id=0)) → None[source]

Close connection to the server

This method performs a connection close handshake with the server, then closes the underlying connection.

If this connection close is due to a client error, the client may provide a reply_code, reply_text, and method_type to indicate to the server the reason for closing the connection.

Parameters:
  • reply_code (int) – the reply code
  • reply_text (str) – localized reply text
  • method_type (amqpy.spec.method_t) – if close is triggered by a failing method, this is the method that caused it
connect() → None[source]

Connect using saved connection parameters

This method does not need to be called explicitly; it is called by the constructor during initialization.

Note: reconnecting invalidates all declarations (channels, queues, consumers, delivery tags, etc.).

drain_events(timeout=None) → None[source]

Wait for an event on all channels

This method should be called after creating consumers in order to receive delivered messages and execute consumer callbacks.

Parameters:timeout (float or None) – maximum allowed time to wait for an event
Raises:amqpy.exceptions.Timeout – if the operation times out
is_alive() → bool[source]

Check if connection is alive

This method is the primary way to check if the connection is alive.

Side effects: This method may send a heartbeat as a last resort to check if the connection is alive.

Returns:True if connection is alive, else False
Return type:bool
loop(timeout=None) → None[source]

Call drain_events() continuously

  • Does not raise Timeout exceptions if a timeout occurs
Parameters:timeout (float or None) – maximum allowed time to wait for an event
send_heartbeat() → None[source]

Send a heartbeat to the server