This code uses the pika library, as recommended by the RabbitMQ developers.
Pika provides a class that can make a connection directly from the connection string provided on the Overview of your deployment. To change the vhost in the connection string, change the final piece of the path to the vhost you wish to connect to.
TLS/SSL is set to "True" along with any additional options. The example sets the TLS version to TLSv1.2, the version supported by Compose.
#!/usr/bin/env python import pika import sys import ssl # connection string and initialization parameters = pika.URLParameters('amqps://user:[email protected]db.com:15845/harmonious-rabbitmq-33') parameters.ssl = True parameters.ssl_options = dict(ssl_version=ssl.PROTOCOL_TLSv1_2) connection = pika.BlockingConnection(parameters) channel = connection.channel() connection = pika.BlockingConnection(parameters) channel = connection.channel() message='This is not a message, this is a pythonic tribute to a message' my_routing_key='tributes' exchange_name='postal' channel.exchange_declare(exchange=exchange_name, exchange_type='direct', durable=True) channel.basic_publish(exchange=exchange_name, routing_key=my_routing_key, body=message) channel.close() connection.close()
The example then connects to the deployment and posts a message to the
postal exchange with the 'tributes' routing key.
If you wish to verify the example after it has run, and see the exchange or consume the message, see the Verifying Connections and Queue Activity page.
If this article didn't solve things, summon a human and get some help!
Updated 2 years ago