如果使用SimpleXMLRPCServer
,则可以覆盖内部_marshaled_dispatch
方法以将信息添加到Fault()
生成的实例中:
这是原始方法:
def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
try:
params, method = xmlrpclib.loads(data)
# generate response
if dispatch_method is not None:
response = dispatch_method(method, params)
else:
response = self._dispatch(method, params)
# wrap response in a singleton tuple
response = (response,)
response = xmlrpclib.dumps(response, methodresponse=1,
allow_none=self.allow_none, encoding=self.encoding)
except:
# report low level exception back to server
# (each dispatcher should have handled their own
# exceptions)
exc_type, exc_value = sys.exc_info()[:2]
response = xmlrpclib.dumps(
xmlrpclib.Fault(1, "%s:%s" % (exc_type, exc_value)),
encoding=self.encoding, allow_none=self.allow_none)
return response
您可以继承SimpleXMLRPCServer.SimpleXMLRPCServer
并覆盖此方法:
import SimpleXMLRPCServer
import sys
import xmlrbclib
class VerboseFaultXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
def _marshaled_dispatch(self, data, dispatch_method = None, path = None):
try:
params, method = xmlrpclib.loads(data)
# generate response
if dispatch_method is not None:
response = dispatch_method(method, params)
else:
response = self._dispatch(method, params)
# wrap response in a singleton tuple
response = (response,)
response = xmlrpclib.dumps(response, methodresponse=1,
allow_none=self.allow_none, encoding=self.encoding)
except:
# report low level exception back to server
# (each dispatcher should have handled their own
# exceptions)
exc_type, exc_value, tb = sys.exc_info()
while tb.tb_next is not None:
tb = tb.tb_next # find last frame of the traceback
lineno = tb.tb_lineno
code = tb.tb_frame.f_code
filename = code.co_filename
name = code.co_name
response = xmlrpclib.dumps(
xmlrpclib.Fault(1, "%s:%s FILENAME: %s LINE: %s NAME: %s" % (
exc_type, exc_value, filename, lineno, name)),
encoding=self.encoding, allow_none=self.allow_none)
return response
然后使用VerboseFaultXMLRPCServer
而不是SimpleXMLRPCServer.SimpleXMLRPCServer
.