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