title: iot edge direct method handler in python
body: <p>I have created a module for a Bacnet scan and it will respond with a list of devices and its address as a result. But I'm having trouble implementing a direct method handler in python. When i first tried implementing it myself i got this error. Which could mean I didn't successfully register the direct method callback. I have some references but it was from C# and azure docs is not helping me figure out the right method to register the callback. for IoTHubModuleClient there's a on_method_request_received and a receive_method_request. appreciate any help!</p>
  <pre><code>def iothub_client_scan_run():
  try:
      iot_client = iothub_client_init()
  
      bacnet_scan_listener_thread = threading.Thread(target=device_method_listener, args=(iot_client,))
      bacnet_scan_listener_thread.daemon = True
      bacnet_scan_listener_thread.start()
  
      while True:
          time.sleep(1000)
      
  def device_method_listener(iot_client):
   while True:
          # Receive the direct method request
      method_request = iot_client.receive_method_request()
      
      print (
          &quot;\nMethod callback called with:\nmethodName = {method_name}\npayload = {payload}&quot;.format(
              method_name=method_request.name,
              payload=method_request.payload
          )
      )
      if method_request.name == &quot;runBacnetScan&quot;:
          response = bacnet_scan_device(method_request)
      else:
          response_payload = {&quot;Response&quot;: &quot;Direct method {} not defined&quot;.format(method_request.name)}
          response_status = 404
              
      # Send a method response indicating the method request was resolved
      print('Sending method response')
      iot_client.send_method_response(response)
      print('Message sent!')    
  </code></pre>
  <p><a href="https://i.stack.imgur.com/JGctz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JGctz.png" alt="enter image description here" /></a></p>
  <p>Edit:
  Here is my route config
  <a href="https://i.stack.imgur.com/idmHs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/idmHs.png" alt="enter image description here" /></a></p>