text: #!/usr/bin/python
  #
  # Licensed to the Apache Software Foundation (ASF) under one
  # or more contributor license agreements.  See the NOTICE file
  # distributed with this work for additional information
  # regarding copyright ownership.  The ASF licenses this file
  # to you under the Apache License, Version 2.0 (the
  # "License"); you may not use this file except in compliance
  # with the License.  You may obtain a copy of the License at
  #
  #   http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing,
  # software distributed under the License is distributed on an
  # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  # KIND, either express or implied.  See the License for the
  # specific language governing permissions and limitations
  # under the License.
  #
  
  from __future__ import print_function
  
  import sys
  
  from proton.handlers import MessagingHandler
  from proton.reactor import Container
  
  class ReceiveHandler(MessagingHandler):
      def __init__(self, conn_url, address, desired):
          super(ReceiveHandler, self).__init__()
  
          self.conn_url = conn_url
          self.address = address
  
          self.desired = desired
          self.received = 0
  
      def on_start(self, event):
          conn = event.container.connect(self.conn_url)
  
          # To connect with a user and password:
          # conn = event.container.connect(self.conn_url, user="<user>", password="<password>")
  
          event.container.create_receiver(conn, self.address)
  
      def on_link_opened(self, event):
          print("RECEIVE: Opened receiver for source address '{0}'".format
                (event.receiver.source.address))
  
      def on_message(self, event):
          message = event.message
  
          print("RECEIVE: Received message '{0}'".format(message.body))
  
          self.received += 1
  
          if self.received == self.desired:
              event.receiver.close()
              event.connection.close()
  
  def main():
      try:
          conn_url, address = sys.argv[1:3]
      except ValueError:
          sys.exit("Usage: receive.py <connection-url> <address> [<message-count>]")
  
      try:
          desired = int(sys.argv[3])
      except (IndexError, ValueError):
          desired = 0
  
      handler = ReceiveHandler(conn_url, address, desired)
      container = Container(handler)
      container.run()
  
  if __name__ == "__main__":
      try:
          main()
      except KeyboardInterrupt:
          pass
  