xmlrpc: remote methods on attributes
Jeff Bauer
jbauer at rubic.com
Thu Apr 27 15:40:19 EDT 2000
Hi all. I've been playing around with PythonWare's
xmlrpclib module.
It's pretty neat, but I want to remotely invoke methods
on attributes. After diddling around and trying to
fool the library, I opted for a more straightforward
approach: a RemoteMethod mixin.
### WARNING: Code for discussion purposes only. ###
from types import FunctionType
class RemoteMethod:
def __init__(self):
self.__methods__ = {}
for n, m in get_members(self.__class__).items():
if n[:2] != '__' and \
type(m) is FunctionType:
self.__methods__[n] = m
def __rcall__(self, method, params):
if self.__methods__.has_key(method):
return apply(self.__methods__[method], (self,)+params)
try:
pos = string.index(method, '.')
except ValueError:
pass
else:
head = method[:pos]
tail = method[pos+1:]
if hasattr(self, head):
return self.__dict__[head].__rcall__(tail, params)
return None
def get_members(_class, members=None):
if members is None:
members = {}
for k in _class.__bases__:
get_members(k, members)
for n, m in vars(_class).items():
if not members.has_key(n):
members[n] = m
return members
########################################################
An example of how an xmlrpc server could be implemented:
class Blarg(RemoteMethod):
def __init__(self):
apply(RemoteMethod.__init__, (self,))
def blarg_method(self, x):
return "blarg_method(%s)" % x
class Server(RemoteMethod, xmlrpc_handler):
def __init__(self):
apply(RemoteMethod.__init__, (self,))
self.blarg = Blarg()
########################################################
In a separate module, the server is remotely invoked.
I map 'call' to '__rcall__' in the top level connection
object, derived from xmlrpclib.
server = Server("http://yadda-yadda")
print server.blarg.blarg_method("Aieee!")
########################################################
I'd be interested in anyone's comments about this
approach, especially from other xmlrpc users. Thanks.
Jeff Bauer
Rubicon Research
More information about the Python-list
mailing list