July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Graham Dumpleton
You need to expose a service on the Web in a way that makes the service accessible to both XML-RPC and SOAP clients.
The OSE package offers a lot of extra flexibility for Python distributed processing, both server-side and client-side. Here is how we can code the actual web service:
# the actual web service, dbwebser.py
# needs the OSE package from http://ose.sourceforge.net
import netsvc
import netsvc.xmlrpc
import netsvc.soap
import signal
import dbm
class Database(netsvc.Service):
def _ _init_ _(self, name):
netsvc.Service._ _init_ _(self, name)
self._db = dbm.open(name,'c')
self.exportMethod(self.get)
self.exportMethod(self.put)
self.exportMethod(self.keys)
self.joinGroup("web-services")
def get(self, key):
return self._db[key]
def put(self, key, value):
self._db[key] = value
def keys(self):
return self._db.keys( )
dispatcher = netsvc.Dispatcher( )
dispatcher.monitor(signal.SIGINT)
httpd = netsvc.HttpDaemon(8000)
database = Database("test")
rpcgw1 = netsvc.xmlrpc.RpcGateway("web-services")
httpd.attach("/xmlrpc/database", rpcgw1)
rpcgw2 = netsvc.soap.RpcGateway("web-services")
httpd.attach("/soap/database", rpcgw2)
httpd.start( )
dispatcher.run( )Here’s a client that accesses the service via XML-RPC:
# dbclient.py # an XML-RPC client using the PythonWare xmlrpclib module (also # included in the standard library with Python 2.2 and later) import xmlrpclib url = "http://localhost:8000/xmlrpc/database/test" ...
Read now
Unlock full access