XMLRPC - persistent object state on server
Demidov Andrey
andrey.demidov at gmail.com
Fri Apr 10 05:33:27 EDT 2009
Hi, all
I need a XMLRPC server, which works with database and returns data to
the clients.
But I can not find any possibility to keep the object state on server
between the clients calls.
Here is my code:
1. Server:
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
# Restrict to a particular path.
class RequestHandler(SimpleXMLRPCRequestHandler):
rpc_paths = ('/RPC2',)
# Create server
server = SimpleXMLRPCServer(("localhost", 8000),
requestHandler=RequestHandler)
server.register_introspection_functions()
class MyClass:
def __init__(self, a):
self.a = a
# and some heavy works which I would like to do once
def say(self):
return a
cl = MyClass(100)
def return_my_value():
return cl.say()
server.register_function(return_my_value, 'r_v')
# Run the server's main loop
server.serve_forever()
2. Client:
import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:8000')
print s.r_v()
When I'm running client I get this error message:
demas at myhost ~/sources/study/python $ python rpc_client.py
Traceback (most recent call last):
File "rpc_client.py", line 4, in <module>
print s.r_v()
File "/usr/lib/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib/python2.6/xmlrpclib.py", line 1253, in request
return self._parse_response(h.getfile(), sock)
File "/usr/lib/python2.6/xmlrpclib.py", line 1392, in _parse_response
return u.close()
File "/usr/lib/python2.6/xmlrpclib.py", line 838, in close
raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: "<type 'exceptions.NameError'>:global name 'a' is
not defined">
How can I fix it? Is there any possibility to keep the object state between
the clients calls?
Thanks,
Demas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090410/eedf205c/attachment.html>
More information about the Python-list
mailing list