Search code examples
pythonencodingxml-rpc

Allowing any ASCII character transmitted in string with xmlrpc


I am using xmlrpc to send a string of characters (arbitrary 8-bit character) from a server to a client. As it turns out, the carriage-return character \r (dec 13 hex 0xd) gets suppressed. That is, when the original string consists of the following three characters (represented by their hex-code):

0x3a 0xd 0xa

then I receive on the client side only 'two' characters:

0x3a 0xa

How can I configure xmlrpc (in python) so I am able to receive the full, original 3-characters string with 0xd? Is there a way this can be done without using a binary wrapper or something else?


Solution

  • When you pass an str object as a string value in XML-RPC, it is encoded and decoded as a string. (BTW, that string should consist of characters allowed in XML, though that is not important in your case.) It is not a binary block, and \r\n is translated to \n when XML is parsed. IMHO, nothing can be done, and no set of options saves the situation.

    Just see what dumps() and loads() from xmlrpclib do to your string:

    >>> from xmlrpclib import dumps, loads
    >>> s = '\x3a\x0d\x0a'
    >>> e = dumps((s,)) # Just a tuple
    >>> e
    '<params>\n<param>\n<value><string>:\r\n</string></value>\n</param>\n</params>\n'
    >>> loads(e)
    ((':\n',), None)
    

    So, when loads() is performed, the \r\n sequence is translated to \n, and \r is lost. You may, of course, use a transformed string (e.g. s.encode('hex') looks quite nice), but, as far as I understand, if you can not alter server behavior (to perform s.decode('hex') on the parameter), nothing can help.