Menu Search

sync_client.py

"""
Demonstrates the client side of the synchronous request-response pattern
(also known as RPC or Remote Procecure Call) using proton.

"""
from __future__ import print_function, unicode_literals

import optparse
from proton import Message, Url, ConnectionException, Timeout
from proton.utils import SyncRequestResponse, BlockingConnection
from proton.handlers import IncomingMessageHandler
import sys

parser = optparse.OptionParser(usage="usage: %prog [options]",
                               description="Send requests to the supplied address and print responses.")
parser.add_option("-a", "--address", default="localhost:5672/examples",
                  help="address to which messages are sent (default %default)")
parser.add_option("-t", "--timeout", type="float", default=5,
                  help="Give up after this time out (default %default)")
opts, args = parser.parse_args()

url = Url(opts.address)
client = SyncRequestResponse(BlockingConnection(url, timeout=opts.timeout), url.path)

try:
    REQUESTS= ["Twas brillig, and the slithy toves",
               "Did gire and gymble in the wabe.",
               "All mimsy were the borogroves,",
               "And the mome raths outgrabe."]
    for request in REQUESTS:
        response = client.call(Message(body=request))
        print("%s => %s" % (request, response.body))
finally:
    client.connection.close()

Download this file