Debian Codegraph @

python/examples/sync_client.py

raw
1 #!/usr/bin/env python3
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 """
22 Demonstrates the client side of the synchronous request-response pattern
23 (also known as RPC or Remote Procedure Call) using proton.
24
25 """
26 import optparse
27 from proton import Message, Url
28 from proton.utils import SyncRequestResponse, BlockingConnection
29
30 parser = optparse.OptionParser(usage="usage: %prog [options]",
31 description="Send requests to the supplied address and print responses.")
32 parser.add_option("-a", "--address", default="localhost:5672/examples",
33 help="address to which messages are sent (default %default)")
34 parser.add_option("-t", "--timeout", type="float", default=5,
35 help="Give up after this time out (default %default)")
36 opts, args = parser.parse_args()
37
38 url = Url(opts.address)
39 client = SyncRequestResponse(BlockingConnection(url, timeout=opts.timeout), url.path)
40
41 try:
42 REQUESTS = ["Twas brillig, and the slithy toves",
43 "Did gire and gymble in the wabe.",
44 "All mimsy were the borogroves,",
45 "And the mome raths outgrabe."]
46 for request in REQUESTS:
47 response = client.call(Message(body=request))
48 print("%s => %s" % (request, response.body))
49 finally:
50 client.connection.close()