This forum has been archived. Please start a new discussion on GitHub.

The problem with strings decoding (Python)

There is a server written in C++ by another developer using ICE 3.7
I do not have the source code of the server, therefore it is impossible to change something in server code.
I am writing a client program in Python. My program get data from server for further work.
I get data using the ice interface in string format.
This is where i have a problem. The server does not write data for the client using utf-8, but uses Winsows-1251.
Because in the received data present the characters of the national alphabet, an exception occurs when decoding in the Python client program
('utf-8' codec can't decode byte 0xd1 in position 3: invalid continuation byte)

Here is the code of functions automatically generated in the Python module using slice2py:

def moduleInfo (self, type, uid, context=None):
    return _M_Server.Server._op_moduleInfo.invoke(self, ((type, uid), context))

Server._op_moduleInfo = IcePy.Operation('moduleInfo', Ice.OperationMode.Normal, Ice.OperationMode.Normal, False, None, (), (((), _M_Server._t_ModuleType, False, 0), ((), IcePy._t_int, False, 0)), (), ((), IcePy._t_string, False, 0), ())

Is it possible to somehow specify the encoding, in which the server sends data, in the client function?
Thanks for your answers. :)


  • xdm
    xdm La Coruña, Spain

    Hi Igor,

    The client cannot specify the server encoding for strings, you will have to install a string converter plug-in to convert a string from and to Windows-1251 encoding.


    The plug-ins are C++ but will work with the Python mapping as it is built on top of the C++ runtime.


  • yaksli
    edited July 2020

    Jose, thank you for your answer. I tried the proposed solution, but it didn't work. I use the windows operating system for development. I used the same code as in the “converter " example from the ice 3.6 repository. This is the Python code, that I used to initialize string converter:

    `“Ice.Plugin.IceStringConverter”, “Ice:createStringConverter windows=1251”)


    communicator = Ice.initialize(sys.argv,initData)`

    Unfortunately, I got the same error. I noticed that the documentation says that the stringConverter works only with Python 2.Is it correct? Maybe that's the problem. I'm using Python 3. Is there any solution of my problem for Python 3?
    Thank you.

  • xdm
    xdm La Coruña, Spain

    I see,

    I first mistunderstood your problem, the string converter will not help you in this case, Ice expects that all the strings on the wire are encoded as UTF8 if an application uses a different encoding for the strings it can use the string converter plug-in to automatically convert the strings to UTF8 before marshal them.

    In python3 you cannot use a string converter because the mapping uses Unicode objects.

    I don't see a workaround for the server sending non-UTF8 strings