FYI: XML-RPC with Medusa

Eugene Leitl eugene.leitl@l...
Wed, 23 May 2001 14:53:39 +0200 (MET DST)


http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52226

Title: XML-RPC with Medusa
By: Jeff Bauer
Last Updated: 2001/03/11
Version no: 1.0


1 vote(s)


Description:

This section demonstrates remote method calls between two
machines (or processes) using the XML-RPC protocol. A complete
example of working client/server code is provided.

Source: Text Source

# xmlrpc_server.py
from socket import gethostname
from medusa.xmlrpc_handler import xmlrpc_handler
from medusa.http_server import http_server
from medusa import asyncore

class xmlrpc_server(xmlrpc_handler):
"""The xmlrpc_server class demonstrates a simple implementation
of Userland's XML-RPC protocol. You must download and install
xmlrpclib and Medusa to run this code.

Obtain Sam Rushing's Medusa library from http://www.nightmare.com
Download Fredrik Lundh's xmlrpclib at http://www.pythonware.com"""

def __init__(self, host=None, port=8182):
if host is None:
host = gethostname()
hs = http_server(host, port)
hs.install_handler(self)
asyncore.loop()

def add(self, op1, op2):
return op1 + op2

def call(self, method, params):
print "call method: %s, params: %s" % (method, str(params))
if method == 'add':
return apply(self.add, params)
return "method not found: %s" % method

if __name__ == '__main__':
server = xmlrpc_server()

---

# xmlrpc_client.py
from socket import gethostname
from xmlrpclib import Transport, dumps

class xmlrpc_connection:
"""The xmlrpc_connection class tests the xmlrpc_server. You must
download and install the medusa and xmlrpclib libraries to run
this code: http://www.nightmare.com http://www.pythonware.com"""

def __init__(self, host=None, port=8182):
if host is None:
host = gethostname()
self.host = "%s:%s" % (host, port)
self.transport = Transport()

def remote(self, method, params=()):
"""remote invokes the server with the method name and an
optional set of parameters. The return value is always a
tuple."""

response = self.transport.request(self.host,
'/RPC2',
dumps(params, method))
return response

if __name__ == '__main__':
connection = xmlrpc_connection()
(answer,) = connection.remote("add", (40, 2))
print "The answer is:", answer


Discussion:

XML-RPC is one of the easier ways to handle distributed
processing tasks. There's no messing around with the low-level
socket details, nor is it necessary to write an interface
definition. The protocol is platform-neutral and
language-neutral. The XML-RPC specification can be
found at: http://www.xml-rpc.com

To run the example above, it is necessary to download
xmlrpclib from http://www.pythonware.com and the Medusa
library from http://www.nightmare.com.

[ Aside: Perhaps someone from ActiveState can provide an
additional client example in Perl, to demonstrate how multiple
languages can be used together with xmlrpc. -Jeff ]