code: def StopService(service_name, service_binary_name=None):
    
    # QueryServiceStatus returns: scvType, svcState, svcControls, err,
    # svcErr, svcCP, svcWH
    try:
      status = win32serviceutil.QueryServiceStatus(service_name)[1]
    except pywintypes.error as e:
      if getattr(e, "winerror", None) == winerror.ERROR_SERVICE_DOES_NOT_EXIST:
        logging.debug("Tried to stop '%s', but the service is not installed.",
                      service_name)
      else:
        logging.exception("Unable to query status of service '%s':", service_name)
      return
  
    for _ in range(20):
      if status == win32service.SERVICE_STOPPED:
        break
      elif status != win32service.SERVICE_STOP_PENDING:
        try:
          win32serviceutil.StopService(service_name)
        except pywintypes.error:
          logging.exception("Unable to stop service '%s':", service_name)
      time.sleep(1)
      status = win32serviceutil.QueryServiceStatus(service_name)[1]
  
    if status == win32service.SERVICE_STOPPED:
      logging.info("Service '%s' stopped.", service_name)
      return
    elif not service_binary_name:
      return
  
    # Taskkill will fail on systems predating Windows XP, this is a best
    # effort fallback solution.
    output = subprocess.check_output(
        ["taskkill", "/im", "%s*" % service_binary_name, "/f"],
        shell=True,
        stdin=subprocess.PIPE,
        stderr=subprocess.PIPE)
  
    logging.debug("%s", output)
  
    # Sleep a bit to ensure that process really quits.
    time.sleep(2)
docs: Stop a Windows service with the given name.
  
    Args:
      service_name: string The name of the service to be stopped.
      service_binary_name: string If given, also kill this binary as a best effort
          fallback solution.