Got unicode error when send string parameter over D-Bus

697 Views Asked by At

I try to use python dbus module to connect to WEP security WiFi

I fill the network configuration dictionary like as follows:

nw_config['wep_key0'] = binascii.unhexlify(mypassword)

mypassword is hex-string

when mypassword is set to '12345678' there will be no error, but when it comes in english letters such as a, b, c, d, e, f.

for instance:

nw_config['wep_key0'] = binascii.unhexlify('abcdef')

It will show the following error

UnicodeError: String parameters to be sent over D-Bus must be valid UTF-8 with no noncharacter code points

Just don't understand what difference between these two cases since they should all be valid hex-string?

Update : The code related to dbus

args = dbus.Dictionary(nw_config)
bus = dbus.SystemBus()
wpas_obj = bus.get_object(WPAS_DBUS_SERVICE, WPAS_DBUS_PATH)
wpas = dbus.Interface(wpas_obj, WPAS_DBUS_SERVICE)
if_obj = bus.get_object(WPAS_DBUS_SERVICE, path)
path = wpas.GetInterface(if_obj, WPAS_DBUS_IFACE)
network = iface.AddNetwork(args)    # this line has problem
2

There are 2 best solutions below

7
Ulrich Eckhardt On

All the letters, but also the hex digits 8 and 9 have their highest bit set. When this occurs in the upper nibble of a byte, this byte can only be part of a multi-byte UTF-8 sequence. Check out the Wikipedia article on UTF-8 to get further explanations.

Now, not every byte sequence is valid UTF-8, and your data probably isn't. Your code doesn't help either, because it doesn't include the data from the file that you're trying to decode. You should be able to reproduce it with something like this:

data = '0123456789abcdef'
bytes = unhexlify(data)
string = bytes.decode('UTF-8')

You should get the error on the third line.

0
Anakin Tung On

Thanks for @Ulrich Eckhardt's help.

Suppose you have a hex string wep key, named pw. Then you have to do the following to success transmit this key in dbus:

dbus.ByteArray(pw.decode('hex'))