import pika
import json
# Connect to RabbitMQ
connection = pika.BlockingConnection(
pika.ConnectionParameters('localhost', 5672)
)
channel = connection.channel()
# Declare a queue (ensure it exists)
channel.queue_declare(queue='my_queue')
# JSON data to publish
data = {'message': 'Hello, sara!', 'data': 42}
# Publish the JSON message
channel.basic_publish(
exchange='', # Use default exchange
routing_key='my_queue',
body=json.dumps(data)
)
print("Message published")
connection.close()
pip install pika jsonlib
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3-management
web dashboard: http://localhost:15672/ username: guest password: guest