mirroring object attributes using xml-rpc

S

sashang

Hi

Say I have a class like the following

class Test:
i = 1

def geti(self):
return self.i

And I use it in an xml-rpc server like this:

t = Test()
s.register_instance(t)

Then the client code can get the value of i like this:
c = xmlrpclib.ServerProxy("address")
c.geti()

but why can't I get the value of i like this?

c.i

How can I implement such behaviour? Moreover, if there were more member
objects in the class Test, how can tell the client 1) that these
attributes exist and 2) how can I use them from the client side without
having to resort to defining a function get_xxx() in the server class?
 
S

sashang

Just to make things clearer the problem I have is if I do this:

c = xmlrpclib.ServerProxy("http://somewhere")
c.i

I get this error:

<Fault 1: "exceptions.TypeError:'int' object is not callable">

So how do I fake things so that xmlrpc knows not to try and call i but
gets the value of i instead?
 
F

Fredrik Lundh

Then the client code can get the value of i like this:
c = xmlrpclib.ServerProxy("address")
c.geti()

but why can't I get the value of i like this?

c.i

you can't. the XML-RPC protocol only supports method calls, not
attribute accesses.
> How can I implement such behaviour?

you could use reflection to automatically wrap attributes on the server
side; e.g.

class foo:
x = 10
# generic getter
def __getattr__(self, key):
if not key.startswith("get_"):
raise AttributeError
value = getattr(self, key[4:])
return lambda: value

f = foo()
print f.x
print f.get_x()

</F>
 
S

skip

sashang> Then the client code can get the value of i like this:
sashang> c = xmlrpclib.ServerProxy("address")
sashang> c.geti()

sashang> but why can't I get the value of i like this?

sashang> c.i

"RPC" stands for "Remote Procedure Call". You're looking for a remote
object access protocol. As Irmen pointed out, Pyro is one such beast,
though it is limited to use with Python. If you need something that's more
language-independent, you might look at SOAP. Personally, XMLRPC has been
sufficient for me for several years.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top