Menu Search

recv_async.py

import sys, optparse
from async import *

parser = optparse.OptionParser(usage="usage: %prog [options] <addr_1> ... <addr_n>",
                               description="simple message receiver")

opts, args = parser.parse_args()

if not args:
  args = ["amqp://~0.0.0.0"]

class App(CallbackAdapter):

    def on_start(self):
        print "Started"
        for a in args:
            print "Subscribing to:", a
            self.messenger.subscribe(a)
        self.messenger.recv()

    def on_recv(self, msg):
        print "Received:", msg
        if msg.body == "die":
            self.stop()
        if msg.reply_to:
            self.message.clear()
            self.message.address = msg.reply_to
            self.message.body = "Reply for: %s" % msg.body
            print "Replied:", self.message
            self.send(self.message)

    def on_stop(self):
        print "Stopped"

a = App(Messenger())
a.run()

Download this file