Friday, November 7, 2008

Remote procedure calls are SO EASY !! (XMLRPC / Python)

I just love python !
And the XMLRPC implementation for python just made me fall in love with it. (Ok, XMLRPC is language independent, but I just loved the python implementation)

A bit of context; we were discussing the paper "Implementing Remote Procedure calls" (Link), in our CS736 / Advanced Operating Systems class. I was skeptical of the whole idea since I was wondering why in the world would anyone use this, when the socket api is so neat and clean.

I was wrong !

My CS740 / Advanced Networks project mate, Ian Rae, pointed me to XMLRPC within the context of our networks project. And I realized how simple and elegant programming RPC can be.

The gist of the code is as follows:
Server:
from SimpleXMLRPCServer import SimpleXMLRPCServer

def poke(n):
print "I was poked %d times" % n
return ("dumb_server", "8000")

server = SimpleXMLRPCServer(("localhost", 8000))
server.register_function(poke, "poke_the_server")
server.serve_forever()
Client:
import xmlrpclib

proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
reply = proxy.poke_the_server(3)
print "The server %s is listening on port %d" % (reply[0], reply[1])
Reference: http://docs.python.org/library/xmlrpclib.html

.

No comments: