Debian Codegraph @

python/examples/server.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 import optparse
22 from proton import Message, Url
23 from proton.handlers import MessagingHandler
24 from proton.reactor import Container
25
26
27 class Server(MessagingHandler):
28 def __init__(self, url, address):
29 super(Server, self).__init__()
30 self.url = url
31 self.address = address
32
33 def on_start(self, event):
34 print("Listening on", self.url)
35 self.container = event.container
36 self.conn = event.container.connect(self.url)
37 self.receiver = event.container.create_receiver(self.conn, self.address)
38 self.server = self.container.create_sender(self.conn, None)
39
40 def on_message(self, event):
41 print("Received", event.message)
42 self.server.send(Message(address=event.message.reply_to, body=event.message.body.upper(),
43 correlation_id=event.message.correlation_id))
44
45
46 parser = optparse.OptionParser(usage="usage: %prog [options]")
47 parser.add_option("-a", "--address", default="localhost:5672/examples",
48 help="address from which messages are received (default %default)")
49 opts, args = parser.parse_args()
50
51 url = Url(opts.address)
52
53 try:
54 Container(Server(url, url.path)).run()
55 except KeyboardInterrupt:
56 pass