Python Manual User Manual

Page of 124
Remote Access
112
Even if a new protocol comes along, it is a relatively simple matter to incorporate yet another gateway,
again without you having to make modifications to the core of your system.
The RPC Gateway
The gateway which accepts an RPC request is actually an instance of a HTTP server object. The gate-
way will accept a request and based on the URL determine which service the request applies to. The
request will then be translated into a call over the service agent framework, with the corresponding re-
sult being packaged up and returned to the remote client.
Because the service agent framework can operate in a distributed manner using the message exchange
framework, the service which a request applies to need not even be in the same process as the RPC
gateway. So that a remote client can’t access any arbitrary service however, a mechanism is provided
to limit which services are actually visible. The mechanisms for client and user authorisation imple-
mented by the HTTP servlet framework can also be used to block access as appropriate.
For the NET-RPC protocol, the RPC gateway is implemented by the
RpcGateway
class. When cre-
ated, the gateway needs to be supplied the name of a service group. Only those services which are a
member of that service group will be accessible through that particular instance of the RPC gateway.
Having created an instance of the RPC gateway, it needs to be mapped into the URL namespace of a
HTTP daemon object.
import netsvc
import signal
class Validator(netsvc.Service):
def __init__(self,name="validator"):
netsvc.Service.__init__(self,name)
self.joinGroup("web-services")
self.exportMethod(self.echo)
def echo(self,*args)
return args
dispatcher = netsvc.Dispatcher()
dispatcher.monitor(signal.SIGINT)
validator = Validator()
port = 8000
group = "web-services"
httpd = netsvc.HttpDaemon(port)
rpcgw = netsvc.RpcGateway(group)
httpd.attach("/service",rpcgw)
httpd.start()
dispatcher.run()