xmlrpclib mapping method names in dictionary

Patrick Price no-email at nowhere.com
Fri Mar 14 17:48:21 CET 2003


I'm new to Python and am just starting to use xmlrpclib for creating a 
simple xml-rpc server to expose certain unix operating system commands.

Currently I am defining each method individually:

def ps(self):
    a = os.popen("ps -eaf")
    l = a.readlines()
    a.close()
    return l

def df(self):
    a = os.popen("df -k")
    l = a.readlines()
    a.close()
    return l

but I notice I'm doing the same thing over and over for each command. 
What I'd like to do is put the command (to become the method or function 
name) along with the actual OS command in a dictionary, such as:

functions={'ps':'ps -eaf', 'df':'df -k'}

and then iterate through the dictionary to register the functions with 
the xmlrpc server.  I have no idea how to do this.

Here's how I currently register a function:


     server=SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 9000))
     server.register_function(lambda astr: '_' + astr, '_string')
     server.serve_forever()

So I want to do something like:

   for cmd in functions:
     server.register(cmd, functions[cmd])

and I think I need to use apply but I have no idea how to go about this.

Can anyone help?  Thanks a million.

-Patrick Price





More information about the Python-list mailing list