rabbit python pub message
#948
- Author
- Anonymous
- Created
- Nov. 20, 2023, 12:59 p.m.
- Expires
- Never
- Size
- 631 bytes
- Hits
- 20
- Syntax
- Python
- Private
- ✗ No
import pika
def publish_message(message):
# Establish a connection to RabbitMQ server
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue named 'hello'
channel.queue_declare(queue='hello')
# Publish the message to the 'hello' queue
channel.basic_publish(exchange='', routing_key='hello', body=message)
print(f" [x] Sent '{message}'")
# Close the connection
connection.close()
if __name__ == '__main__':
message_to_publish = "Hello, RabbitMQ!"
publish_message(message_to_publish)