I have a simple xmlrpc
server defined as (server.py
):
from SimpleXMLRPCServer import SimpleXMLRPCServer
def foo():
return "foo"
server = SimpleXMLRPCServer(("localhost", 1025))
server.register_introspection_functions()
server.register_function(foo, 'test.foo')
server.serve_forever()
and the client (client.py
) implemented as follows:
import xmlrpclib
class XMLRPC(object):
def __init__(self):
self.xmlrpc = xmlrpclib.ServerProxy("http://localhost:1025/RPC2")
def __getattr__(self, name):
attr = getattr(self.xmlrpc, name)
def wrapper(*args, **kwargs):
result = attr(*args, **kwargs)
return result
return wrapper
xmlrpc = XMLRPC()
print xmlrpc.test.foo()
I want to wrap and execute each call that is being made to the xmlrpc
server within the class XMLRPC
. But the above working example gives an error
Traceback (most recent call last):
File "client.py", line 20, in <module>
xmlrpc.test.foo()
AttributeError: 'function' object has no attribute 'foo'
How to make this code work?
Additional information and constraints:
- I already tried to wrap
wrapper
withfunctools.wraps(attr)
without succcess. As the stringattr
has no attribute__name__
I get a different error - I canot change anything defined in
server.py
. - The above example is fully working.
- Replacing
return wrapper
byreturn attr
is not a solution - I need to execute the actualxmlrpc
call withinwrapper
. - I need a simple solution without a 3rd party library, standard python libs are ok.