Python Manual User Manual

Page of 124
The Client Application
113
In this example, any HTTP request made using a URL whose path falls under the base URL of "
ht-
tp://localhost:8000/service/
", will be regarded as being a NET-RPC request. The name
of the service which a request applies to is determined by removing the base URL component from the
full URL. The full URL used to access the service in this example would therefore be "
http://lo-
calhost:8000/service/validator
". Note that the service is only visible however, because
it had added itself to the group "web-services", the same group as the RPC gateway had been initialised
with.
The methods of the service which are available are the same as those which would be accessible over
the service agent framework internal to your application. That is, a service must export a method for it
to be accessible. The only such method available in this example would be "
echo()
".
The Client Application
Client side access to the NET-RPC protocol is available through the Python "
netrpc
" module. This
module is not dependent on the "
netsvc
" module and is pure Python. The name of the class used to
make a request to a remote service is
RemoteService
. This class behaves in a similar fashion to the
LocalService
class from the "
netsvc
" module except that the service name is replaced with the
URL identifying the remote service.
import netrpc
url = "http://localhost:8000/service/validator"
service = netrpc.RemoteService(url)
print service.echo(1,1L,1.1,"1")
Only the "http" protocol is supported. If the URL specifies an unsupported protocol, the exception
Ad-
dressInvalid
will be raised. If the URL didn’t identify a valid service on the remote host, a
ServiceUnavailable
exception is raised. Other possible exceptions which may be raised are
AuthenticationFailure
and
TransportFailure
. All the more specific exceptions actually
derive from
ServiceFailure
and the
ServiceFailure
exception is also used for errors gen-
erated by the service itself, so it is often sufficient to watch out for just that type of exception.
Restricting Client Access
In addition to being able to dictate precisely which services are visible, it is also possible to restrict
access to specific clients. This can be done by allowing only certain hosts access, or by limiting access
to specific individuals by using user authentication. Both schemes rely on features within the existing
HTTP servlet framework.
class HttpDaemon(netsvc.HttpDaemon):
def __init__(self,port,hosts=["127.0.0.1"]):
netsvc.HttpDaemon.__init__(self,port)
self._allow = hosts
def authorise(self,host):