xmlrpc Server

Andrew ajwms at visto.com
Sun Jun 3 11:21:23 EDT 2001


I am somewhat a newbie at python programming, so please be gentle...:)

I have been experimenting with xmlrpc servers programming with
pythonware's xmlrpc library and by using the examples written by Dave
Warner at:  http://www.onlamp.com/pub/a/python/2001/01/17/xmlrpcserver.html


Everything works well; until I start modifying the code :)...

Here is a quick excerpt of what I've done (minus Dave's sample
methods):

----------------------------------------------------------------
import SocketServer
import xmlrpcserver
import xmlrpclib

class TestRequestHandler(xmlrpcserver.RequestHandler):
    #Override method:
    def __init__(self,a,b,c):
        self.x = 0
        xmlrpcserver.RequestHandler.__init__(self,a,b,c)

    def call(self, method, params):
        print "Dispatching: ", method, params
        try:
            server_method = getattr(self, method)
        except:
            raise AttributeError, "Server does not contain XML-RPC
procedure %s" % method
        return server_method(method, params)

    def add(self, method, params):
        self.x = self.x + int(params[0])
        return "Sum = %s" % str(self.x)    

if __name__ == '__main__':
    server = SocketServer.TCPServer(('', 8000), TestRequestHandler)
    server.serve_forever()
----------------------------------------------------------------


I run the above code, then create a server object and run the add
method
tst = xmlrpclib.Server("http://localhost:8000")
print tst.add(1)
-->Sum = 1

cool, looking good.

print tst.add(2)
-->Sum = 2

huh?  Shouldn't this value be 3?


Why is self.x being re-initalized whenever I call the tst.add method? 
Shouldn't the __init__ method be called once setting self.x to 0 and
then the add method simple adds to that value keeping track of the
total?

Am I completely missing the boat here? 

TIA,

Andrew



More information about the Python-list mailing list