code: def shutdown_host(port, username=None, password=None, authdb=None):
      
      host = 'localhost:%i' % port
      try:
          mc = MongoConnection(host)
          try:
              if username and password and authdb:
                  if authdb != "admin":
                      raise RuntimeError("given username/password is not for "
                                         "admin database")
                  else:
                      try:
                          mc.admin.authenticate(name=username, password=password)
                      except OperationFailure:
                          # perhaps auth is not required
                          pass
  
              mc.admin.command('shutdown', force=True)
          except AutoReconnect:
              pass
          except OperationFailure:
              print("Error: cannot authenticate to shut down %s." % host)
              return
  
      except ConnectionFailure:
          pass
      else:
          mc.close()
docs: Send the shutdown command to a mongod or mongos on given port.
  
      This function can be called as a separate thread.