text: # -------------------------------------------------------------------------
  # Copyright (c) Microsoft Corporation. All rights reserved.
  # Licensed under the MIT License. See License.txt in the project root for
  # license information.
  # --------------------------------------------------------------------------
  
  import os
  import asyncio
  from six.moves import input
  import threading
  from azure.iot.device.aio import IoTHubDeviceClient
  
  
  async def main():
      # The connection string for a device should never be stored in code. For the sake of simplicity we're using an environment variable here.
      conn_str = os.getenv("IOTHUB_DEVICE_CONNECTION_STRING")
      # The client object is used to interact with your Azure IoT hub.
      device_client = IoTHubDeviceClient.create_from_connection_string(conn_str)
  
      # connect the client.
      await device_client.connect()
  
      # define behavior for receiving a message
      # NOTE: this could be a function or a coroutine
      def message_received_handler(message):
          print("the data in the message received was ")
          print(message.data)
          print("custom properties are")
          print(message.custom_properties)
          print("content Type: {0}".format(message.content_type))
          print("")
  
      # set the mesage received handler on the client
      device_client.on_message_received = message_received_handler
  
      # define behavior for halting the application
      def stdin_listener():
          while True:
              selection = input("Press Q to quit\n")
              if selection == "Q" or selection == "q":
                  print("Quitting...")
                  break
  
      # Run the stdin listener in the event loop
      loop = asyncio.get_running_loop()
      user_finished = loop.run_in_executor(None, stdin_listener)
  
      # Wait for user to indicate they are done listening for messages
      await user_finished
  
      # Finally, disconnect
      await device_client.disconnect()
  
  
  if __name__ == "__main__":
      asyncio.run(main())
  
      # If using Python 3.6 or below, use the following code instead of asyncio.run(main()):
      # loop = asyncio.get_event_loop()
      # loop.run_until_complete(main())
      # loop.close()
  