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.