import unicodedata


def sanitize_ssid(ssid):
  """Remove control and non-UTF8 characters from an SSID.

  We use hostapd's utf8_ssid option to specify a UTF8 SSID.

  Args:
    ssid: The SSID to sanitize, as a string.

  Returns:
    The sanitized SSID, as a UTF8-encoded string.
  """
  return ''.join(c for c in ssid.decode('utf-8', 'ignore')
                 if unicodedata.category(c)[0] != 'C').encode('utf-8')