July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Brian Quinlan
You need to implement an XML-RPC server.
The xmlrpclib package also makes writing XML-RPC
servers pretty easy. Here’s how you can write an
XML-RPC server:
# server coder sxr_server.py
# needs Python 2.2 or the XML-RPC package from PythonWare
import SimpleXMLRPCServer
class StringFunctions:
def _ _init_ _(self):
# Make all of the Python string functions available through
# python_string.func_name
import string
self.python_string = string
def _privateFunction(self):
# This function cannot be called directly through XML-RPC because
# it starts with an underscore character '_', i.e., it's "private"
pass
def chop_in_half(self, astr):
return astr[:len(astr)/2]
def repeat(self, astr, times):
return astr * times
if _ _name_ _=='_ _main_ _':
server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", 8000))
server.register_instance(StringFunctions( ))
server.register_function(lambda astr: '_' + astr, '_string')
server.serve_forever( )And here is a client that accesses the server you just wrote:
# server coder sxr_client.py
# needs Python 2.2 or the XML-RPC package from PythonWare
import xmlrpclib
server = xmlrpclib.Server('http://localhost:8000')
print server.chop_in_half('I am a confidant guy')
print server.repeat('Repetition is the key to learning!\n', 5)
print server._string('<= underscore')
print server.python_string.join(['I', 'like it!'], " don't ")
print server._privateFunction( ) # will throw an exceptionThis ...