SimpleXMLRPCServer请求调度问题

发布于 2024-11-17 07:14:01 字数 947 浏览 2 评论 0原文

我们正在开发基于客户端-服务器 XML-RPC 的应用程序。服务器部分应该根据每个请求知道每个客户端的 IP 地址。

为了实现这一点,我们将 SocketServer.ThreadingMixIn 混合到 SimpleXMLRPCServer 中,并子类 SimpleXMLRPCRequestHandler 来重新定义它的 _dispatch 方法。代码如下:

class ThreadedXMLRPCServer(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)

问题在于,有时请求IP地址和请求数据都被混淆处理,即请求IP地址与真实地址不匹配。

_dispatch 的最后一行有问题还是我们遗漏了什么?

谢谢!

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 ThreadedXMLRPCServer(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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2024-11-24 07:14:01

您可能应该重新引发在自定义 _dispatch 方法中调用 function(ip_addr ...) 时遇到的任何异常,否则您将面临短路内置错误处理的风险。

这就是我的意思……

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

不过,如果它与您的问题有关,我会感到惊讶。据我所知,你所拥有的应该可以正常工作。

出于好奇,如果您继承 ForkingMixin 会发生什么?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文