SimpleXMLRPCServer请求调度问题
我们正在开发基于客户端-服务器 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能应该重新引发在自定义
_dispatch
方法中调用function(ip_addr ...)
时遇到的任何异常,否则您将面临短路内置错误处理的风险。这就是我的意思……
不过,如果它与您的问题有关,我会感到惊讶。据我所知,你所拥有的应该可以正常工作。
出于好奇,如果您继承 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 ...
... 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?