import string from twisted.application import service, internet from twisted.internet import reactor, protocol from twisted.internet.protocol import Protocol, Factory port = 80 class AirChat(Protocol): username = "" session = False def connectionMade( self ): self.factory.numProtocols = self.factory.numProtocols+1 self.factory.addClient( self ) def connectionLost( self , reason ): self.factory.numProtocols = self.factory.numProtocols-1 self.factory.delClient( self ) def dataReceived( self , data ): # parse out HTTP a = data.split( '\x00' )[0].split(' ') # look for HTTP header if a[0] == 'GET' or a[0] == 'POST': self.transport.write( """HTTP/1.1 200 OK\r\n\r\n""" ) self.transport.write( """AIR Chat 1.0""" ) self.transport.loseConnection() return a = data.split( '\x00' )[0].split( '||' ) if a[0] == '1': self.factory.sendAll( '1||' + self.factory.stripChars( a[1] ) + '||' + self.factory.stripChars( a[2] ) + '\x00' ) elif a[0] == '2': self.transport.write( '2||' + str( self.factory.numProtocols ) + '\x00') class AirChatFactory(Factory): protocol = AirChat numProtocols = 0 clients = [] def stripChars(self, textIn): textIn = textIn.replace('<', '') textIn = textIn.replace('', '') textIn = textIn.replace('/>', '') return textIn def addClient(self, newclient): self.clients.append( newclient ) def delClient(self, client): self.clients.remove( client ) def sendAll(self, message): for proto in self.clients: proto.transport.write( message ) class AirChatService( internet.TCPServer ): def __init__(self): internet.TCPServer.__init__(self, 80, AirChatFactory())