-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend.py
22 lines (17 loc) · 960 Bytes
/
send.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
#We're connected now, to a broker on the local machine - hence the localhost.
# If we wanted to connect to a broker on a different machine we'd simply specify its name or IP address here.
#Next, before sending we need to make sure the recipient queue exists.
# If we send a message to non-existing location, RabbitMQ will just drop the message.
# Let's create a hello queue to which the message will be delivered:
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
#Before exiting the program we need to make sure the network buffers were flushed and our message was actually delivered to RabbitMQ.
# We can do it by gently closing the connection.
connection.close()