socialwifi / RouterOS-api

Python API to RouterBoard devices produced by MikroTik.
MIT License
258 stars 98 forks source link

Unicode Decode Error (UTF-8) #68

Open l-portugal opened 3 years ago

l-portugal commented 3 years ago

My script obtain a list of DHCP Server leases. When i run the script, this show the error:

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 6: invalid continuation byte"

the command is :

cmd = api.get_resource('/ip/dhcp-server/lease') leases = cmd.get()

In leases, some characters in "host-name" field they may be: á,é,í,ó,ú

is there any way to fix this error without change the characters in host-name field?

The script is in python3.

Thanks.

skeapskeap commented 3 years ago

Try to change decode params of method get_python_value in routeros_api/api_structure.py There is bytes.decode() by default. Try bytes.decode('cp1251')

def get_python_value(self, bytes):
        return bytes.decode('cp1251')
l-portugal commented 3 years ago

Try to change decode params of method get_python_value in routeros_api/api_structure.py There is bytes.decode() by default. Try bytes.decode('cp1251')

def get_python_value(self, bytes):
        return bytes.decode('cp1251')

Thanks skeapskeap. I solve it changing bytes.decode() to bytes.decode("utf-8", "ignore")

routeros_api/api_structure.py:

def get_python_value(self, bytes):
        return bytes.decode("utf-8", "ignore")

thanks

colinhd8 commented 3 years ago

I have the same issue, thank you for your comment.

Try to change decode params of method get_python_value in routeros_api/api_structure.py There is bytes.decode() by default. Try bytes.decode('cp1251')

def get_python_value(self, bytes):
        return bytes.decode('cp1251')

Thanks skeapskeap. I solve it changing bytes.decode() to bytes.decode("utf-8", "ignore")

routeros_api/api_structure.py:

def get_python_value(self, bytes):
        return bytes.decode("utf-8", "ignore")

thanks

Gunni commented 1 year ago

I had the same problem, patched it on my side using: return bytes.decode('iso-8859-1') but would like an official fix.