Created
July 5, 2009 18:55
-
-
Save anotherjesse/141080 to your computer and use it in GitHub Desktop.
experimenting with amqp from python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment