Recipe 15.4. Enabling an XML-RPC Server to Be Terminated Remotely
Credit: Christoph Dietze, Brian Quinlan, Jeff
Bauer
Problem
You are coding an XML-RPC server, using
the Python Standard Library's
SimpleXMLRPCServer module, and you want to make it
possible for a remote client to cause the XML-RPC server to exit
cleanly.
Solution
You have to use your own request-handling loop (instead of the
serve_forever method of
SimpleXMLRPCServer) so that you can stop looping
when appropriate. For example:
import SimpleXMLRPCServer
running = True
def finis( ):
global running
running = False
return 1
server = SimpleXMLRPCServer.SimpleXMLRPCServer(('127.0.0.1', 8000))
server.register_function(finis)
while running:
server.handle_request( )
Discussion
SimpleXMLRPCServer's
serve_forever method, as its name implies,
attempts to keep serving
"forever"that is, it keeps
serving until the whole server process is killed. Sometimes,
it's useful to allow remote clients to request a
clean termination of a service by remotely calling a server-exposed
function, and this recipe demonstrates the simplest way to allow this
functionality.The finis function (which gets exposed to remote
clients via the register_function call) sets the
global variable running to False
(and then returns something that is not None
because the XML-RPC protocol cannot deal with the
None object). Using the while
running loop, instead of a serve_forever
call, then ensures that the server stops serving and terminates when
the variable running becomes false.If you prefer to subclass SimpleXMLRPCServer, you
can obtain a similar effect by overriding the
serve_forever method: that is, instead of placing
the simple while running:
server.handle_request loop inline, you can code, for
example (with the same function finis as in the
recipe's Solution):
class MyServer(SimpleXMLRPCServer.SimpleXMLRPCServer):However, this alternative approach offers no special advantage
def serve_forever(self):
while running:
self.handle_request( )
server = MyServer(('127.0.0.1', 8000))
server.register_function(finis)
server.serve_forever( )
(unless you have a fetish for being object oriented for no particular
purpose), and, since this alternative approach is telling a little
white lie (by using the name serve_forever for a
method that does not keep serving
"forever"!), the simpler approach
in the recipe's Solution can definitely be
recommended.
See Also
The SimpleXMLRPCServer module is part of the
Python Standard Library and is documented in a chapter of the
Library Reference portion of
Python's online documentation.