[Tutor] Doubt in RPC XML lib

Kent Johnson kent_johnson at skillsoft.com
Mon Sep 13 14:20:26 CEST 2004


Varun,

You can use eval() or getattr() to do what you want. For example, with your 
server running on port 8000, I can do this:
 >>> import xmlrpclib
 >>> server = xmlrpclib.ServerProxy('http://localhost:8000')
 >>> server.system.listMethods()
['add', 'pow', 'system.listMethods', 'system.methodHelp', 
'system.methodSignature']
 >>> server.add(1, 2)
3
 >>> eval('server.add(1, 2)')
3
 >>> getattr(server, 'add')(1,2)
3

I suggest you change your data structure to hold the actual server object 
and the names of the methods. Then you can use getattr to get the function 
object from the server and call it.

I would create a list of (server, list of functions) where server is the 
actual server object, i.e. something like
servers = [ (server1, ['add', 'pow']), (server2, ['foo', 'bar']) ]

Then given serverNum, funcNum and args you can do
server, funcNames = servers[serverNum]
funcName = funcNames[funcNum]
getattr(server, funcName)(args)


By the way instead of
         for host in range(0,len(self.alivehosts)):
             hostname="http://"+self.alivehosts[host]+":8000"

you can say
         for host in self.alivehosts:
             hostname="http://"+host+":8000"

Kent

At 05:08 PM 9/13/2004 +0530, Varun Soundararajan wrote:
>I hv attached the two files. Actually what i am trying to do is that,
>i replicate the gethosts.py and servers.py code in every system. in
>every system when i start my python code, i check all servers of the
>same type and add to my active hosts list. ( i do taht in gethosts.py)
>in the servers.py, i serve my own server at 10001 and rpc server at
>8000 using thread. My server at 10001 authenticates with other server
>of my type by sending some hashed data and getting it back (just to
>ensure that i am not adding some other server listening at 10001).
>Now my qn is,
>if i want to access a code i call:
>server.somefunction()
>now since i have the list of functions of form (['1.fn1',
>'1.fn2','2.fn1' etc] ) when i want to call the fn1 of server 0 i shd
>call
>hosts_server_access[0].fn1() . but i want the user to choose that. Now
>how do i do that.
>in other words.
>if i wanna call fn1 of server 0 i shd be able to call(say)
>hosts_server_access[0][1]() or something like that.
>pls help
>thanks in advance
>-Varun
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list