Twisted is a networking engine written in Python, supporting numerous protocols. It contains a web server, numerous chat clients, chat servers, mail servers, and more. For more information see http://twistedmatrix.com/trac/wiki/TwistedProject

Server prototype:

#!/usr/bin/env python

# import twisted modules
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Protocol, Factory
from twisted.enterprise import adbapi, row, reflector
from twisted.enterprise.sqlreflector import SQLReflector
from twisted.python import log

# import system modules
import os, sys, string

class DBSServer:
   def __init__(self,url="",port=""):
       factory = Factory()
       factory.protocol = DBSManager
       reactor.listenTCP(port,factory)
       reactor.run()


class DBSManager(Protocol):
   def __init__(self):
#       self.dbpool = adbapi.ConnectionPool("cx_Oracle", "cms_dbs_afaq/Me1tabOlia6s@devdb10")
#       self.dbpool = adbapi.ConnectionPool("DCOracle2", "cms_dbs_afaq/Me1tabOlia6s@devdb10")
       self.dbpool = adbapi.ConnectionPool("sqlite", database='/home/vk/CMS/DBS/work/sqlite.db')
       self.result = ""

   def _getData(self,txn):
       txn.execute("SELECT * FROM t_primary_dataset")
       result = txn.fetchone()
       print "_getData::result",result
       msg = " I query DB, here is result '%s' -- '%s'"%(result[0],result[1])
#       self.result=result
       self.transport.write(msg)

   def getData(self):
       return self.dbpool.runInteraction(self._getData)

   def dataReceived(self, data):
       self.getData()
       print self.result
       res=data
       print "res",res
       if self.result:
          print "test"
          res+=" I query DB, here is result '%s' -- '%s'"%(self.result[0],self.result[1])
          print res
       self.transport.write(res)
#
# main
#
if __name__ == "__main__":
    server = DBSServer("localhost",27986)

and client code

#!/usr/bin/env python

"""
An example client. Run simpleserv.py first before running this.
"""

from twisted.internet import reactor, protocol


# a client protocol

class EchoClient(protocol.Protocol):
    """Once connected, send a message, then print the result."""

    def connectionMade(self):
        print "client:connectionMade"
        self.transport.write("hello, world!")

    def dataReceived(self, data):
        "As soon as any data is received, write it back."
        print "Server said:", data
        self.transport.loseConnection()

    def connectionLost(self, reason):
        print "connection lost"

class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient

    def clientConnectionFailed(self, connector, reason):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print "Connection lost - goodbye!"
        reactor.stop()

# this connects the protocol to a server runing on port 8000
def main():
    f = EchoFactory()
    reactor.connectTCP("localhost", 27986, f)
    reactor.run()

# this only runs if the module was *not* imported
if __name__ == '__main__':
    main()

-- ValentinKuznetsov - 05 May 2006

This topic: HEP/SWIG > WebHome > CmsDbs > WebServicesTools > Twisted
Topic revision: 05 May 2006, ValentinKuznetsov
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding CLASSE Wiki? Send feedback