-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrabbit.py
39 lines (31 loc) · 1.11 KB
/
rabbit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import pika
class Rabbit():
global message
def sendMessage(self):
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
connection.close()
def readMessage(self):
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
self.message = body
channel.stop_consuming()
channel.basic_consume(callback,
queue='hello',
no_ack=True)
channel.start_consuming()
def relayMessage(self):
self.readMessage()
return self.message
if __name__ == "__main__":
rabbit = Rabbit()
rabbit.sendMessage()
print(rabbit.readMessage())