我正在学习python并尝试创建一个简单的XML-RPC服务器。 Python文档中的示例显示了这一点:
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
requestHandler=RequestHandler)
server.register_introspection_functions()
# Register pow() function; this will use the value of
# pow.__name__ as the name, which is just 'pow'.
server.register_function(pow)
# Run the server's main loop
server.serve_forever()
所以我想做的是将SimpleXMLRPCServer
扩展为MyXMLRPC
,将其中一些方法设置为服务器方法。这些是对象方法,而不仅仅是静态/类方法,因此它们必须能够使用MyXMLRPC
实例数据进行操作。我认为这会有点使用这个功能:
# Register an instance; all the methods of the instance are
# published as XML-RPC methods (in this case, just 'div').
class MyFuncs:
def div(self, x, y):
return x // y
server.register_instance(MyFuncs())
但当然我只想注册一些方法,而且,我真的需要在MyXMLRPC
的构造函数中执行此操作。我该怎么做?