I have a xmlrpc
server running looking like the following
from SimpleXMLRPCServer import SimpleXMLRPCServer
def add(x,y):
return x+y
server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(add, 'add.numbers')
server.serve_forever()
which is called used within the following code:
import xmlrpclib
class DeviceProxy(object):
def __init__(self, uri):
self.rpc = xmlrpclib.ServerProxy(uri)
def __getattr__(self, attr):
return getattr(self.rpc, attr)
original = DeviceProxy.__getattr__
def mygetattr(device, attr):
def wrapper(*args, **kw):
print('called with %r and %r' % (args, kw))
return original(device, attr)(*args, **kw)
return wrapper
DeviceProxy.__getattr__ = mygetattr
dev = DeviceProxy("http://localhost:8000/RPC2")
print dev.add.numbers(4,6)
As you can see, the Proxy
class wraps the xmlrpc
proxy for reasons outside the scope of this question, forwarding arbitrary calls via the __getattr__
method . For further reasons outside the scope for this question, I need to wrap/replace this __getattr__
method by a different method to e.g. print out the name of the function called, the arguments etc. (see related question here).
But this approach does not work, it gives the following error:
AttributeError: 'function' object has no attribute 'numbers'
The example works as expected when I
- do not replace
DeviceProxy.__getattr__
with something else replace
DeviceProxy.__getattr__
with the functiondef dummy(instance, attr): return original(device,attr)
replace the name of the
xmlrpc
function by a zero-dotted name (e.g. justsum
instead ofsum.numbers
)
You can verify yourself that the following, direct call via the xmlrpc proxy will work as expected:
dev = xmlrpclib.ServerProxy("http://localhost:8000/RPC2")
print dev.add.numbers(4,6)
My question: How to solve my problem, i.e. how to be able to wrap/overwrite the DeviceProxy.__getattr__
correctly to be able to see the function called, all arguments etc WITHOUT making changes in the xmlrpc
server or the DeviceProxy
class?