July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Jeff Bauer
You need to establish a distributed processing system and want to use the XML-RPC protocol.
The medusa package lets you implement lightweight,
highly scalable servers, even with old versions of Python. An XML-RPC
handler is included in the Medusa distribution. Here is how you code
a server with Medusa:
# 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):
# initializes and runs the server
def _ _init_ _(self, host=None, port=8182):
if host is None:
host = gethostname( )
hs = http_server(host, port)
hs.install_handler(self)
asyncore.loop( )
# an example of a method to be exposed via the XML-RPC protocol
def add(self, op1, op2):
return op1 + op2
# the infrastructure ("plumbing") to expose methods
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( )And here is an
xmlrpclib-based client of this server:
# xmlrpc_client.py from socket import gethostname from xmlrpclib import Transport, dumps class xmlrpc_connection: 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=( ...