Recipe 15.4. Enabling an XML-RPC Server to Be Terminated RemotelyCredit: Christoph Dietze, Brian Quinlan, Jeff Bauer ProblemYou 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. SolutionYou 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
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):
def serve_forever(self):
while running:
self.handle_request( )
server = MyServer(('127.0.0.1', 8000))
server.register_function(finis)
server.serve_forever( )
However, this alternative approach offers no special advantage (unless you have a
See AlsoThe 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. |
Recipe 15.5. Implementing SimpleXMLRPCServer NicetiesCredit: Rune Hansen ProblemYou are coding XML-RPC servers with the Python Standard Library SimpleXMLRPCServer class and want to ensure you're using the simple but useful idioms that can ease your coding, or give your servers more flexibility at no substantial cost to you. Solution
Here are a few tweaks I
# give the base class a short, readable nickname
from SimpleXMLRPCServer import SimpleXMLRPCServer as BaseServer
class Server(BaseServer):
def _ _init_ _(self, host, port):
# accept separate hostname and portnumber and group them
BaseServer._ _init_ _(self, (host, port))
def server_bind(self):
# allow fast restart of the server after it's killed
import socket
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
BaseServer.server_bind(self)
allowedClientHosts = '127.0.0.1', '192.168.0.15',
def verify_request(self, request, client_address):
# forbid requests except from specific client hosts
return client_address[0] in self.allowedClientHosts
Discussion
The recipe begins with a statement of the form
from
module
import
The sole purpose of the
_ _init_ _
statement of class
Server
is to accept
host
and
port
as separate parameters and group them into the required tuple. I find
By default, a server socket
Last but not least, the recipe
See AlsoThe 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. |