Chapter 6. Network connections
6.1. Connection URLs
Connection URLs encode the information used to establish new connections.
Connection URL syntax
Scheme - The connection transport, either
amqpfor unencrypted TCP or
amqpsfor TCP with SSL/TLS encryption.
- Host - The remote network host. The value can be a hostname or a numeric IP address. IPv6 addresses must be enclosed in square brackets.
Port - The remote network port. This value is optional. The default value is 5672 for the
amqpscheme and 5671 for the
Connection URL examples
amqps://example.com amqps://example.net:56720 amqp://127.0.0.1 amqp://[::1]:2000
6.2. Creating outgoing connections
To connect to a remote server, call the
Container.connect() method with a connection URL. This is typically done inside the
Example: Creating outgoing connections
class ExampleHandler(MessagingHandler): def on_start(self, event): event.container.connect("amqp://example.com") def on_connection_opened(self, event): print("Connection", event.connection, "is open")
See the Chapter 7, Security section for information about creating secure connections.
6.3. Configuring reconnect
Reconnect allows a client to recover from lost connections. It is used to ensure that the components in a distributed system reestablish communication after temporary network or component failures.
AMQ Python enables reconnect by default. If a connection is lost or a connection attempt fails, the client will try again after a brief delay. The delay increases exponentially for each new attempt, up to a default maximum of 10 seconds.
To disable reconnect, set the
reconnect connection option to
Example: Disabling reconnect
To control the delays between connection attempts, define a class implementing the
next() methods and set the
reconnect connection option to an instance of that class.
Example: Configuring reconnect
class ExampleReconnect(object): def __init__(self): self.delay = 0 def reset(self): self.delay = 0 def next(self): if self.delay == 0: self.delay = 0.1 else: self.delay = min(10, 2 * self.delay) return self.delay container.connect("amqp://example.com", reconnect=ExampleReconnect())
next method returns the next delay in seconds. The
reset method is called once before the reconnect process begins.
6.4. Configuring failover
AMQ Python allows you to configure multiple connection endpoints. If connecting to one fails, the client attempts to connect to the next in the list. If the list is exhausted, the process starts over.
To specify multiple connection endpoints, set the
urls connection option to a list of connection URLs.
Example: Configuring failover
urls = ["amqp://alpha.example.com", "amqp://beta.example.com"] container.connect(urls=urls)
It is an error to use the
urls options at the same time.
6.5. Accepting incoming connections
AMQ Python can accept inbound network connections, enabling you to build custom messaging servers.
To start listening for connections, use the
Container.listen() method with a URL containing the local host address and port to listen on.
Example: Accepting incoming connections
class ExampleHandler(MessagingHandler): def on_start(self, event): event.container.listen("0.0.0.0") def on_connection_opened(self, event): print("New incoming connection", event.connection)
The special IP address
0.0.0.0 listens on all available IPv4 interfaces. To listen on all IPv6 interfaces, use
For more information, see the server receive.py example.