Python XMLRPC question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Oct 15 05:18:24 CEST 2009


En Wed, 14 Oct 2009 22:08:09 -0300, prasanna <prasanna at ix.netcom.com>  
escribió:

> 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 xmlrpcshutdown.py ===
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):
         pass

     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')
     server.register_function(server.shutdown)
     server.serve_forever()

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"
     server.shutdown()

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


C:\TEMP>start python xmlrpcshutdown.py server

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

C:\TEMP>

-- 
Gabriel Genellina




More information about the Python-list mailing list