1 """The most basic chat protocol possible.
3 run me with twistd -y chatserver.py, and then connect with multiple
4 telnet clients to port 1025
7 from twisted.protocols import basic
11 class MyChat(basic.LineReceiver):
12 def connectionMade(self):
13 print "Got new client!"
14 self.factory.clients.append(self)
16 def connectionLost(self, reason):
17 print "Lost a client!"
18 self.factory.clients.remove(self)
20 def lineReceived(self, line):
21 print "received", repr(line)
22 for c in self.factory.clients:
25 def message(self, message):
26 self.transport.write(message + '\n')
29 from twisted.internet import protocol
30 from twisted.application import service, internet
32 factory = protocol.ServerFactory()
33 factory.protocol = MyChat
36 application = service.Application("chatserver")
37 internet.TCPServer(1025, factory).setServiceParent(application)