Python’s xmlrpclib – A Simple Example

I used XML-RPC for the first time today. I didn’t really know what it was so I was basically just scratching around in the dark. Alas, here’s what I found out so far:

  1. XML-RPC is a web service type of deal; A protocol if you will similar in purpose to SOAP or other such things.
  2. The communication happens pretty simply. Function calls and return values are transferred over HTTP and wrapped in XML somehow.
  3. Python has a library called xmlrpclib that seems to do all the heavy lifting for you. See sample code below.
  4. All of the Python examples you find out there, yeah those won’t work for you! It seems that the functions exposed by each URI are unique so you just need to find out what functions are available.
  5. Is there a standard way to find out what functions are available? I don’t know yet.

Here is some sample Python code for connecting to the XML-RPC interface over at the UPC database:

>>> import xmlrpclib
>>> server=xmlrpclib.ServerProxy('http://www.upcdatabase.com/rpc')
>>> print server.help()

help() - returns string
	Show available functions and their parameters.

lookupUPC(upc string) - returns struct
	Lookup upc database entry.

calculateCheckDigit(upc string) - returns string
	Parameter 'upc' should have 'C' in place of
	the check digit.

convertUPCE(upce string) - returns string
	Parameter 'upce' should be exactly 8 digits.
	Returns UPC Type A equivalent.

decodeCueCat(cuecatscan string) - returns struct
	Returns serial number, type, and code given
	CueCat scanner output.

All 'upc' parameters should be 12 digits long.

>>> server.lookupUPC('071641301627')
{'description': '30162-SH 2PK BL FINE SHARPIE', 'lastModified': '2005-10-21 15:02:06', 'upc': '071641301627', 'isCoupon': False, 'pendingUpdates': 0, 'found': True, 'message': 'Database entry found', 'size': '2 PACK'}

Why am I connecting to the UPC database? You may be wondering. Well, let’s just say the curious had better keep an eye on my product announcements page ;-)

Here are some useful pages where I learned everything I know about XML-RPC:
http://www-128.ibm.com/developerworks/webservices/library/ws-pyth10.html
http://www.xml.com/pub/rg/XML_and_Python_Tutorials
http://www.onlamp.com/pub/a/python/2000/11/22/xmlrpcclient.html
http://docs.python.org/lib/module-xmlrpclib.html
http://www.xmlrpc.com/

And here are some search terms I used. Placed here in the hope that some poor sap finds his way here instead of wondering for two hours why print server.examples.getStateName(41) doesn’t work!

  1. xmlrpc
  2. xmlrpclib
  3. xmlrpclib tutorial
  4. python XML-RPC
  5. XML-RPC HowTo

[tags]Python, XML-RPC, XMLRPC, xmlrpclib, web services, upc database, upc[/tags]

One Response to “Python’s xmlrpclib – A Simple Example”