簡體   English   中英

我們可以在xml rpc服務器中注冊多少個類/實例?

[英]how many classes/instances can we register into xml rpc server?

我正在嘗試在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()

這是我的客戶代碼 -

import 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

我有與你相同的經驗,它在文檔中並不完全明確,但只能注冊一個實例。

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

class AllFuncs(MyFuncs, MyFuncs2):
    pass

server.register_instance(AllFuncs)

我實際上發現,如果你查看代碼,這在模塊定義中會提到,但是,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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM