Re: simple example with xmlrpc server

[email protected]
Newsgroups gmane.comp.python.twisted.web
Message-ID <20110416214113.1992.1003242500.divmod.xquotient.530@localhost.localdomain>
On 09:24 pm, [email protected] wrote:
>
>I have simple example with xmlrpc server from standart python:
>
>from SimpleXMLRPCServer import SimpleXMLRPCServer
>from time import sleep
>import threading,time
>
>class Test(threading.Thread):
>    def __init__(self):
>        threading.Thread.__init__(self)
>        self.test1 = 0
>    def test(self):
>        return self.test1
>
>    def run(self):
>        while(1):
>            time.sleep(1)
>            self.test1 = self.test1 + 1
>
>ts = Test()
>ts.start()
>server =  SimpleXMLRPCServer(("localhost",8888))
>server.register_instance(ts)
>server.serve_forever()
>
>how it will look on Twisted ?

Something like this:

  from twisted.internet import reactor
  from twisted.internet.task import LoopingCall
  from twisted.web.xmlrpc import XMLRPC
  from twisted.web.server import Site

  class Counter(XMLRPC):
      def __init__(self):
          XMLRPC.__init__(self)
          self.value = 0
          LoopingCall(self._update).start(1, now=False)

      def _update(self):
          self.value += 1

      def xmlrpc_test(self):
          return self.value

  factory = Site(Counter())
  reactor.listenTCP(8888, factory, interface="localhost")
  reactor.run()

Jean-Paul
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.