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

George Patterson george at visp.com.au
Thu Apr 15 22:24:37 MDT 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!"
        self.factory.clients.append(self)

    def connectionLost(self):
        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 + '\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