0

I've got an application where I need to scan surrounding WiFi networks and provide them to a user, and I've got some troubles for decoding WiFi SSID list, as I always end up with special characters not properly decoded.

Here is the function I'm using:

def get_ssid_list():
    '''get list of available ssids, using unix utility iwlist. Returns the populated list'''
    result = subprocess.run(["iw", WLAN_ITF, "scan"], capture_output=True)
    iwlist_result = result.stdout.decode("utf-8")
    ssid_regexp = re.compile("SSID: (.*)\n", re.MULTILINE)
    ssid_list = list(set(ssid_regexp.findall(iwlist_result)))
    ssid_list = [ssid for ssid in ssid_list if ('x00' not in ssid) and ssid != '']
    ssid_list = [ssid.replace("\\x20", " ") for ssid in ssid_list]
    if not ssid_list:
        return None
    else:
        ssid_list.sort(key=str.lower)
        return ssid_list

The problem is, with network like "Bifröst", I end up with b"Bifr\\xc3\\xb6st", which gives me "Bifr\xc3\xb6st" as string. I spent some amount of time the previous days on this, but can't find my solution..

I tried using encode() and decode() functions with different encoding format, as well as str.replace(), but can't remove the extra "\" that explains the problem (I found that b"Bifr\xc3\xb6st" is properly decoded as "Bifröst"). (Ps: I also had some troubles writing this post with "\" everywhere ^^)

Any help would be really appreciate.

Piocky
  • 11
  • 3
  • [bytes.decode()](https://docs.python.org/3/library/stdtypes.html#bytes.decode) will convert those UTF-8 ***bytes*** to a Unicode ***string*** (of "code points"). – Joop Eggen Jun 30 '23 at 11:06
  • @JoopEggen as explained, when I use decode() function on "b"Bifr\\xc3\\xb6st" I end up with "Bifr\xc3\xb6st" as string – Piocky Jul 03 '23 at 08:13

1 Answers1

1

I found the solution here, thanks to @user19087. I need to use codecs.escape_decode(bytes(myString, "utf-8"))[0].decode("utf-8"), which gives this in my function:

def get_ssid_list():
    '''get list of available ssids, using unix utility iwlist. Returns the populated list'''
    result = subprocess.run(["iw", "wlan0", "scan"], capture_output=True)
    iwlist_result = result.stdout.decode("utf-8")
    ssid_regexp = re.compile("SSID: (.*)\n", re.MULTILINE)
    ssid_list = list(set(ssid_regexp.findall(iwlist_result)))
    ssid_list = [codecs.escape_decode(bytes(ssid, "utf-8"))[0].decode("utf-8") for ssid in ssid_list if ('x00' not in ssid) and ssid != '']
    if not ssid_list:
        return None
    else:
        ssid_list.sort(key=str.lower)
        return ssid_list
Piocky
  • 11
  • 3