Python Manual Benutzerhandbuch

Seite von 124
Invalid Request Method
69
client = self.currentRequest().sender()
self._count = self._count + 1
name "%s/%d" % (self.serviceName(),self._count)
session = Session(name,client)
return name
Such a mechanism as described can’t be used if such a request to create a session may originate over
an RPC over HTTP connection. This is because the service agent which acts as proxy for the request
is transient and will be destroyed once the request has completed. Further, the service agent which acts
as proxy isn’t visible outside of its own process.
The alternative to binding the session to a particular service agent is to create a pseudo unique name
for the service managing the session. To ensure that the session object is destroyed, a timer could be
used to trigger the destruction of the service after a certain period of inactivity. Each request made
against the service would reset the timer giving it a new lease on life. The timeout may be something
which is fixed or which could be defined as one of the arguments supplied in the request to create the
session.
Invalid Request Method
When a service request arrives with a method name which the service doesn’t provide, the member
function "
invalidMethod()
" is called. By default this method will generate a failure response
with origin of "
netsvc
" and error code of "
SERVER_METHOD_UNAVAILABLE
". This member
function might be overridden if the ability to dump out information about requests against invalid
methods was wanted. Any derived implementation of this member function should still call the base
class version to generate the appropriate failure response indicating a method was unavailable.
class Service(netsvc.Service):
# ...
def invalidMethod(self,methodName,params):
print methodName,params
netsvc.service.invalidMethod(methodName,params)
Local Service Requests
Use of the interface described so far for initiating a service request is the preferred interface. This is
because it will not matter if the service to which the request is being sent is in the same process or an-
other process. It also will not matter if the service is written in the same language or a different lan-
guage. However, when the service is in the same process and is also written in Python a short cut is
available. This will avoid the complexity of using delayed responses, but does mandate that the service
being called always be in the same process.
Access to this short cut is through the Python class
LocalService
. An instance of the class is cre-
ated with the name of the service against which the call is to be made. A call is then made against the
object as if it were the actual service. This is the same as when a service endpoint object is used except