2003-10-09 19:41

Nifty python/xmlrpc thing

Using python, it's trivial to turn any module into a XMLRPC server. So, if you are careful writing your app in a modular way, you can move the backend elsewhere in very little code.

For example, if I had a module that has some functionality, like this (module.py):

#Stupid simple module

def add(a,b):
        return a+b

A program would use it like this:

import module
print module.add(2,4)

Now, if I move the module to a remote box (server), create this xmlrpc server program in the server (and run it ;-):

#!/usr/bin/env python

#Simple generic XML-RPC server

import module

import SimpleXMLRPCServer

server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 800
0))

server.register_instance(module)
server.serve_forever()

And this bridge module in the client (clientmodule.py):

import xmlrpclib

server = xmlrpclib.Server('http://remotserver.com:8000')

Now this very slightly modified version of our program works in exactly the same way as the original:

from clientmodule import server as module

print module.add(2,4)

Of course this has issues:

  • It has no security
  • XMLRPC could introduce errors and the program doesn't catch them.
  • It could be made even more transparent with some more pythoning

But anyway, I think it's cute.

Comments

Comments powered by Disqus

Contents © 2000-2018 Roberto Alsina