Python XMLRPC question

Gabriel Genellina gagsl-py2 at
Thu Oct 15 05:18:24 CEST 2009

En Wed, 14 Oct 2009 22:08:09 -0300, prasanna <prasanna at>  

> Out of curiosity--one more thing I haven't yet figured out, is there a
> xmlrpc command I can send that stops or restarts the server?

If you're using Python 2.6, the easiest way is to register its shutdown()  
method. Note that it *must* be called from a separate thread (just inherit  
 from ForkingMixIn)

On earlier versions, overwrite the serve_forever loop (so it reads `while  
not self._quit: ...`) and add a shutdown() method that sets self._quit to  
True. You'll need to call shutdown twice in that case.

=== begin ===
import sys

def server():
     from SocketServer import ThreadingMixIn
     from SimpleXMLRPCServer import SimpleXMLRPCServer

     # ThreadingMixIn must be included when publishing
     # the shutdown method
     class MyXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer):

     print 'Running XML-RPC server on port 8000'
     server = MyXMLRPCServer(("localhost", 8000),
         logRequests=False, allow_none=True)
         # allow_none=True because of shutdown
     server.register_function(lambda x,y: x+y, 'add')

def client():
     from xmlrpclib import ServerProxy

     print 'Connecting to XML-RPC server on port 8000'
     server = ServerProxy("http://localhost:8000")
     print "2+3=", server.add(2, 3)
     print "asking server to shut down"

if sys.argv[1]=="server": server()
elif sys.argv[1]=="client": client()
=== end ===

C:\TEMP>start python server

C:\TEMP>python client
Connecting to XML-RPC server on port 8000
2+3= 5
asking server to shut down


Gabriel Genellina

More information about the Python-list mailing list