You can download this code by clicking the button below.
This code is now available for download.
This function uses the PyAMQP library to create a connection to RabbitMQ, declares an exchange, and publishes a message to this exchange with a routing key and a message body. Finally, it closes the connection.
Technology Stack : PyAMQP, RabbitMQ
Code Type : Function
Code Difficulty : Intermediate
def random_exchange_publish(exchange, routing_key, body, mandatory=False):
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange=exchange, exchange_type='direct', durable=True)
channel.basic_publish(exchange=exchange, routing_key=routing_key, body=body, mandatory=mandatory)
connection.close()