You can download this code by clicking the button below.
This code is now available for download.
This function connects to a RabbitMQ server, declares an exchange, and sends a message with a specific routing key.
Technology Stack : PyAMQP, RabbitMQ
Code Type : Function
Code Difficulty : Intermediate
def exchange_info(exchange_name, routing_key, message):
import pika
import random
# Create a connection to the RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare an exchange
channel.exchange_declare(exchange=exchange_name, exchange_type='direct')
# Publish a message to the exchange with a specific routing key
channel.basic_publish(exchange=exchange_name, routing_key=routing_key, body=message)
# Close the connection
connection.close()