1

XML-RPC は初めてです。

#client code
import xmlrpclib
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
x,y = arg1,arg2
print proxy.fun1(x,y)
print proxy.fun2(x,y)

サーバーがすべきこと:

  1. fun1 をロード
  2. fun1を登録する
  3. 結果を返す
  4. fun1 をアンロードする

そしてfun2で同じことをします。

これを行う最善の方法は何ですか?

私はこれを行う方法を考え出しましたが、それは「不器用で、大げさで、非パイソン的」に聞こえます。

4

3 に答える 3

2

関数を動的に登録できます (サーバーの起動後)。

#Server side code:
import sys
from SimpleXMLRPCServer import SimpleXMLRPCServer

def dynRegFunc(): #this function will be registered dynamically, from the client
     return True 

def registerFunction(function): #this function will register with the given name
    server.register_function(getattr(sys.modules[__name__], str(function)))

if __name__ == '__main__':
    server = SimpleXMLRPCServer((address, port)), allow_none = True)
    server.register_function(registerFunction)
    server.serve_forever()



#Client side code:

 import xmlrpclib

 if __name__ == '__main__':
     proxy = xmlrpclib.ServerProxy('http://'+str(address)+':'+str(port), allow_none = True)

 #if you'd try to call the function dynRegFunc now, it wouldnt work, since it's not registered -> proxy.dynRegFunc() would fail to execute

 #register function dynamically: 
  proxy.registerFunction("dynRegFunc")
 #call the newly registered function
  proxy.dynRegFunc() #should work now!
于 2014-08-08T11:44:29.677 に答える
2

動的登録が必要な場合は、オブジェクトのインスタンスを登録してから、そのオブジェクトに属性を設定します。__getattr__実行時に関数を決定する必要がある場合は、クラスのメソッドを使用してさらに高度にすることができます。

class dispatcher(object): pass
   def __getattr__(self, name):
     # logic to determine if 'name' is a function, and what
     # function should be returned
     return the_func
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(dispatcher())
于 2009-01-31T20:07:49.690 に答える