gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
write python code to send messages to rabbitmq using the pika module
- User
write python code to send messages to rabbitmq using the pika module
- Assistant
Sure! Below is an example of Python code that uses the Pika module to send messages to RabbitMQ:
pythonimport pika # Establish a connection to RabbitMQ server connection = pika.BlockingConnection(pika.ConnectionParameters('localhost')) channel = connection.channel() # Create a queue to send messages to channel.queue_declare(queue='my_queue') # Define the message to send message = 'Hello, RabbitMQ!' # Publish the message to the queue channel.basic_publish(exchange='', routing_key='my_queue', body=message) # Close the connection connection.close()Make sure you have the
pikamodule installed before running this code. You can install it usingpip install pika.In this example, we establish a connection to the RabbitMQ server running on
localhost. Then, we create a queue named to send messages to. Next, the message is defined. Finally, the message is sent to the queue using the method, and the connection is closed.