Recently I have been contemplating client-server applications, how these can eventually be tied into a web app and how best to write all of this in Python. This post will outline the usage of one of the more awesome XMLRPC libraries and also discuss why it’s better than what else is out there…
So, the first place that good ol’ Google too me was the XMLRPC library for Python, however – may I recommend to steer clear of this library as it suffers from the following most immediate problems:
- Complex python objects cannot be passed easily, not without writing your own picking
- It presents errors when handling types it can;t marshal – things such as lists and dictionaries, which, out of pure interest are possibly some of the more useful elements in python
- No traceback for errors – this is a biggie, debugging an RPC client/server app is a pain in a** when you can;t clearly identify which part of the server code is failing since all your errors are coming from the XMLRPC library.
So, enough about that. If you are interested in torturing yourself with XMLRPC, you can find some really nice tutorials from IBM here on building a simple client / server.
Having given up on XMLRPC, I swiftly moved to the rather wonderful PYRO library. This library has great documentation, a clear syntax and some very handy features. Particularly, I would really enjoy:
- When you pass objects from client to server, they are full-blown python objects that retain all properties, not some poxy proxy
- It can handle all those un-marshalable types, so passing a really complex custom class down the wire isn’t a problem
- Full blown exception handling – you can trap the traceback of the error to your server app with a really nice error handler
So, without further ado – here’s the simplest way to use the PYRO library (this is pretty much a copy of the main example – I’ve just expanded with some additional notes underneath to save everyone some frustration):
This is our server application
#You can do what you like in here to inittialise your server
# However, it's worth remembering to rais PYRO's own
# init() method!
def whatsMyName(self, name):
# This is a service we are going to make public
return "Your name is %s, hi %s!" % (name, name)
We'll start the server with this function
# Get the server ready
# Create a daemon object - you can set host and port here too
# This is the URI of the service once it launches
print "Server URI is:", uri
# Start the server...
if __name__ == "__main__":
As you can see from the code above, it’s very similar to the XMLRPC implementation of a service – essentially, all you need to do is create a class that represents your server, add some methods that do fancy things and return some kind of object. Unlike XMLRPC, PYRO will not crash if your method returns None.
Once you have created the server, the startServer() function is only used to kick off the daemon serving loop, an interesting thing to note here is the URI that the daemon spits out, because it will look horrendous and changes each time the server boots up. Now this might seem like quite silly behaviour, but it only occurs because there are different ways of accessing PYRO objects from a client, and the difference in the access URI’s left me with quite a bit of confusion for a while. But all will become clear.
Part 2 – The client:
Create a new client.py file and stick this code into it:
proxy = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/jobserver")
print "Calling remote function whatsMyName()..."
return_value = ""
# This is really handy for debugging your server code:
return_value = proxy.whatsMyName('Jively')
Simples! So, what’s happening in this piece of code?
After importing the library, we create a proxy object – you will never call an object directly on the server – all calls are handled through a proxy. In pythonic terms, this is essentially the same as creating a local object based on the aService() class with all calls being called remotely (thanks to inheriting the server class from the Pyro ObjBase class).
You then treat the methods of the server class as if they were local – in this case we call whatsMyName() and it will return the string “Your name is Jively, Hi Jively!” and print this out.
What is important to note and to include is the structure of the try/except exception handling – this wonderful bit of code will make it super simple to debug your server app.
If you’ve worked with Pyro or think there’s any really useful resources out there – let me know in the comments!