开发者

SimpleXMLRPCServer request dispatching issue

We're developing client-server XML-RPC based application. Server part should know IP address of each client on per request basis.

To accomplish this we mix SocketServer.ThreadingMixIn into SimpleXMLRPCServer and subclass SimpleXMLRPCRequestHandler to redefine it's _dispatch method. Below is the code:

class ThreadedX开发者_运维技巧MLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer.SimpleXMLRPCServer):
    pass

class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
    def _dispatch(self, method, params):
        function = self.server.funcs[method]

        def decor(function, ip_addr):
            def new_function(*args):
                try:
                    return function(ip_addr, *args)
                except Exception, err:
                    log_msg('Exception ocurred in XMLRPC thread (%s)!' % err)

            return new_function

        return decor(function, self.client_address[0])(*params)

The problem is that sometimes request IP addresses and request data are all processed mixed up, i. e. request IP address doesn't match it's real address.

Is there some problem with the last line of _dispatch or do we miss something?

Thanks!


You should probably re-raise any exceptions encountered by calling function(ip_addr ...) in your custom _dispatch method, otherwise you risk short-circuiting the built-in error handling.

Here's what I mean ...

class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
    def _dispatch(self, method, params):
        function = self.server.funcs[method]

        def decor(function, ip_addr):
            def new_function(*args):
                try:
                    return function(ip_addr, *args)
                except Exception, err:
                    log_msg('Exception ocurred in XMLRPC thread (%s)!' % err)
                    raise # <---- re-raise

            return new_function

... although, I'd be surprised if it's related to your issue. What you have should work fine, as far as I can tell.

Out of curiosity, what happens if you subclass ForkingMixin instead?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜