[Twisted-Python] explanation of the chatserver.py example

George Patterson george at visp.com.au
Fri Apr 16 00:24:37 EDT 2004

I have trying to write a chat server that will selectively send out messages to some clients. Anyway, looking around for a framework to start from and learn more about Python and other modules I stumbled upon chatserver.py twisted.sourceforge.net

But I can't find what some of the aspects the MyChat class does.

From: http://twisted.sourceforge.net/TwistedDocs-1.2.0/examples/chatserver.py

class MyChat(basic.LineReceiver):
    def connectionMade(self):
        print "Got new client!"

    def connectionLost(self):
        print "Lost a client!"

    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:

    def message(self, message):
        self.transport.write(message + '\n')

#end of class

If Bob, Jane and Joe are chatting to each other, and Bob sends a string of characters to Jane as a private message, how would I loop through the lineReceived definition for Jane's client ID or IP address or something unique to Jane's connection.

I want to change def lineReceived to something like 

def lineReceived(self, line):
    print "received", repr(line)
    for c in self.factory.clients:
	if c.id==required_id: 
	# ^-- I know this is not valid code 

	    c.message("Privmsg". line)
	#end if
    #end for

Sorry if it is a stupid question but I have been staring long enough at the code. 

George Patterson

More information about the Twisted-Python mailing list