SimpleXMLRPCServer and creating a new object on for each new client request.

Martin P. Hellwig martin.hellwig at dcuktec.org
Thu May 7 01:13:53 CEST 2009


Jelle Smet wrote:
> Hi list,
> 
> My goals is to have concurrent and separated client sessions using xmlrpc.
> Initially my though was that SimpleXMLRPCServer was able to create a new
> object instance for each incoming request.
> But this doesn't appear to be the case, unless I'm overlooking something,
> if so please point me out.
> 
> Concider following simplified code
> 
<cut code>
SimpleXMLRPCServer, is rather, well eh, simple and does not support 
sessions, which in an HTTP point of view makes sense (where XML-RPC is 
based on). So either build in some sort of session management or rewrite 
your functionality in such a ways that it isn't dependent on sessions 
(see my reimplementation after my sig).

-- 
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'

#! /usr/bin/env python
#'----------------example reimplementation----------------------------'
"XML RPC test"
import SocketServer
import SimpleXMLRPCServer
import random
import xmlrpclib
import threading


# Create a simple example class
class Randomizer(object):
     "Randomizer Docstring"
     def __init__(self):
         self.random_number = None

     def randomize(self):
         "create a new random number"
         self.random_number = random.randrange(0, 100000)

     def show_random(self):
         "show_random Docstring"
         self.randomize()
         return(self.random_number)

class Server(SocketServer.ThreadingMixIn,
              SimpleXMLRPCServer.SimpleXMLRPCServer):
     "Multi-threaded XML RPC Server"
     # pylint: disable-msg=R0904
     def __init__(self):
         address = ('localhost', 8000)
         SimpleXMLRPCServer.SimpleXMLRPCServer.__init__(self, address)
         self.allow_none = True
         self.register_instance(Randomizer())
         self.register_function(self.shutdown_service)
         self.register_introspection_functions()
     # pylint: enable-msg=R0904

     def shutdown_service(self):
         "Stop server"
         self.shutdown()


class Thread(threading.Thread):
     "Run Server in a thread for testing purposes"
     def run(self):
         "Run the service"
         service = Server()
         service.serve_forever()

if __name__ == '__main__':
     URL_PORT = 'http://localhost:8000'
     TEST_THREAD = Thread()
     TEST_THREAD.start()
     SESSION1 = xmlrpclib.ServerProxy(URL_PORT)
     SESSION2 = xmlrpclib.ServerProxy(URL_PORT)
     print(SESSION1.show_random())
     print(SESSION2.show_random())
     SESSION_STOP = xmlrpclib.ServerProxy(URL_PORT)
     SESSION_STOP.shutdown_service()
#'----------------example reimplementation----------------------------'



More information about the Python-list mailing list