1

我正在尝试在 xml rpc 服务器模块中创建 2 个分类,然后将这两个类的实例注册到 xml rpc 服务器。单独注册时,我可以从这两个实例中运行方法,但是当我仅在其中一个实例上运行时,获取注册而另一个抛出错误。此外,我只能看到我最后注册的实例的类的方法。我想知道号码上有没有石灰。我可以在服务器中注册的实例,因为我记得读过类似的内容,但我现在在文档中找不到提及?

这是我的服务器代码 -

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)
#server.register_instance(FileOperation)
server.register_instance(file)
# Register a function under a different name
def adder_function(x,y):
    return x + y
server.register_function(adder_function, 'add')

# 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
class MyFuncs2:
    def modulus(self, x, y):
        return x % y
server.register_instance(MyFuncs2())
server.register_instance(MyFuncs())

# Run the server's main loop
server.serve_forever()

这是我的客户代码 -

导入 xmlrpclib

try:
    #s = xmlrpclib.ServerProxy(r'http://administrator:passw0rd@172.19.201.59:8000')
    s = xmlrpclib.ServerProxy(r'http://localhost:8000')
    print s.system.listMethods()
    print s.pow(2,3)  # Returns 2**3 = 8
    print s.add(2,3)  # Returns 5
    print s.div(5,2)  # Returns 5//2 = 2
    print s.moduls(5,2)
except Exception,err:
    print err
4

2 回答 2

4

我和你有同样的经历,文档中没有完全明确,但只能注册一个实例。

您仍然可以执行以下操作:

class AllFuncs(MyFuncs, MyFuncs2):
    pass

server.register_instance(AllFuncs)
于 2013-12-04T09:37:06.970 回答
1

实际上,如果您查看代码,我发现模块定义中提到了这一点,但是,python 网站上的文档中没有提到这一点。这是完整的描述 - 注册一个实例来响应 XML-RPC 请求。

Only one instance can be installed at a time.

If the registered instance has a _dispatch method then that
method will be called with the name of the XML-RPC method 
 and
its parameters as a tuple
e.g. instance._dispatch('add',(2,3))

If the registered instance does not have a _dispatch method
then the instance will be searched to find a matching method
and, if found, will be called. Methods beginning with an '_'
are considered private and will not be called by
SimpleXMLRPCServer.

If a registered function matches a XML-RPC request, then it
will be called instead of the registered instance.

If the optional allow_dotted_names argument is true and the
instance does not have a _dispatch method, method names
containing dots are supported and resolved, as long as none of
the name segments start with an '_'.

*** SECURITY WARNING: ***

Enabling the allow_dotted_names options allows intruders
to access your module's global variables and may allow
intruders to execute arbitrary code on your machine.  Only
use this option on a secure, closed network.
于 2013-12-04T09:59:05.880 回答