xmlrpclib and carriagereturn (\r)

Ben Cartwright bencvt at gmail.com
Sat Mar 18 10:46:14 CET 2006

Jonathan Ballet wrote:
> The problem is, xmlrpclib "eats" those carriage return characters when
> loading the XMLRPC request, and replace it by "\n". So I got "bla\n\nbla".
> When I sent back those parameters to others Windows clients (they are
> doing some kind of synchronisation throught the XMLRPC server), I send
> to them only "\n\n", which makes problems when rendering strings.

Did you develop the Windows client, too?  If so, the client-side fix is
trivial: replace \n with \r\n in all renderable strings.  Or update
both the client and the server to encode the strings, also trivial
using the base64 module.

If not, and you're in the unfortunate position of being forced to
support buggy third-party clients, read on.

> It seems that XMLRPC spec doesn't propose to eat carriage return
> characters : (from http://www.xmlrpc.com/spec)
> It seems to be a rather strange comportement from xmlrpclib. Is it known ?
> So, what happens here ? How could I solve this problem ?

The XMLRPC spec doesn't say anything about CRs one way or the other.
Newline handling is necessarily left to the XML parser implementation.
In Python's case, xmlrpclib uses the xml.parsers.expat module, which
reads universal newlines and writes Unix-style newlines (\n).  There's
no option to disable this feature.

You could modify xmlrpclib to use a different parser, but it would be
much easier to just hack the XML response right before it's sent out.
I'm assuming you used the SimpleXMLRPCServer module.  Example:

from SimpleXMLRPCServer import *

class MyServer(SimpleXMLRPCServer):
    def _marshaled_dispatch(self, data, dm=None):
        response = SimpleXMLRPCDispatcher._marshaled_dispatch(self,
data, dm)
        return response.replace('\n', '\r\n')

server = MyServer(('localhost', 8000))
server.register_function(lambda x: x, 'echo')

Hope that helps,

More information about the Python-list mailing list