anotherjesse (owner)

Revisions

gist: 141080 Download_button fork
public
Description:
experimenting with amqp from python
Public Clone URL: git://gist.github.com/141080.git
Embed All Files: show embed
pups.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
 
from amqplib import client_0_8 as amqp
import sys
 
class Pups:
    def __init__(self, hostname):
        self.conn = amqp.Connection(host="%s:5672" % hostname,
                                    userid="guest",
                                    password="guest",
                                    virtual_host="/",
                                    insist=False)
        self.chan = self.conn.channel()
 
        self.setup()
 
    def setup(self):
        self.chan.queue_declare(queue="po_box",
                                durable=True,
                                exclusive=False,
                                auto_delete=False)
 
        self.chan.exchange_declare(exchange="sorting_room",
                                   type="direct",
                                   durable=True,
                                   auto_delete=False)
 
        self.chan.queue_bind(queue="po_box",
                             exchange="sorting_room",
                             routing_key="chat")
 
    def send(self, note):
        print "SEND: %s" % note
        msg = amqp.Message(note)
        msg.properties["delivery_mode"] = 2
        self.chan.basic_publish(msg,
                                exchange="sorting_room",
                                routing_key="chat")
 
    def recv(self):
        msg = self.chan.basic_get("po_box")
        if msg:
            print "RECV: %s" % msg.body
            self.chan.basic_ack(msg.delivery_tag)
        else:
            print "EMPTY"
 
if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage: %s hostname [message]" % sys.argv[0]
        exit(1)
    pups = Pups(sys.argv[1])
    note = ' '.join(sys.argv[2:])
    if note:
        pups.send(note)
    else:
        pups.recv()