aio-pika
aio-pika copied to clipboard
No SSL context support
Why aio-pika doesn't support ssl_context like aiormq? That's necessary to be used to connect to cloud AMQP services such as AWS RabbitMQ.
Let's monkey-patch the class until we receive any response :)
Since the connect method accepts connection_class, we can feed a monkey-patched Connection class that passes ssl_context when calling the deeper connect. Downside is that we may have to follow any future updates to the method though. This code successfully made connection to my AWS Rabbitmq broker in a private VPC via an SSH tunnel.
class CustomConnection(aio_pika.Connection):
async def connect(self, timeout=None) -> None:
transport = await aio_pika.abc.UnderlayConnection.connect(
self.url, self._on_connection_close,
timeout=timeout,
# Modified this line
**dict(**self.kwargs, context=ssl_context)
)
await self._on_connected(transport)
self.transport = transport
async def connect_async():
connect = await aio_pika.connect(host='localhost', port=5671, login='guest', password='guest', ssl=True,
connection_class=CustomConnection)
channel = await connect.channel()
await channel.declare_queue('hello')
Actually inheritance is not a monkey-patch.
Oh, yes, you're right :)