July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Graham Dumpleton
You need to allow some distributed services to supply methods, and other distributed services to access and use those methods, in a location-independent way by writing a suitable central exchange (middleware) server.
The OSE package also supports the request/reply architecture. First, we need a central middleware process to which all others connect:
# The central.py script -- needs the OSE package from http://ose.sourceforge.net import netsvc import signal dispatcher = netsvc.Dispatcher( ) dispatcher.monitor(signal.SIGINT) exchange = netsvc.Exchange(netsvc.EXCHANGE_SERVER) exchange.listen(11111) dispatcher.run( )
Next, we need a server that supplies methods through the middleware:
# The server.py script -- needs the OSE package from http://ose.sourceforge.net
import netsvc
import signal
class Service(netsvc.Service):
def _ _init_ _(self):
netsvc.Service._ _init_ _(self, "math")
self.joinGroup("web-services")
self.exportMethod(self.multiply)
def multiply(self, x, y):
return x*y
dispatcher = netsvc.Dispatcher( )
dispatcher.monitor(signal.SIGINT)
exchange = netsvc.Exchange(netsvc.EXCHANGE_CLIENT)
exchange.connect("localhost",11111,5)
service = Service( )
dispatcher.run( )Then, we need a client that consumes methods through the middleware:
# The client.py script -- needs the OSE package from http://ose.sourceforge.net import netsvc import signal import random class Client(netsvc.Service): ...
Read now
Unlock full access