[Twisted-Python] LineReceiver

Jean-Paul Calderone exarkun at divmod.com
Sun Feb 25 18:45:35 EST 2007

On Sun, 25 Feb 2007 18:26:33 -0500, Lee Connell <lee.a.connell at gmail.com> wrote:
>Hello all,
>I am building a basic chat program.  I am using LineReceiver for the
>protocol on both the server and the client.  My problem is when i loop
>through all the clients in my ServerFactory and choose to send data to the
>client with multiple calls to transport.write(data), the client side does
>not pick it up in two seperate calls, instead it is received in one
>lineReceive. In the server I call transport.write() twice, the client
>however receives both calls in one lineReceive instead of two lineRecieve
>calls, how can I get lineReceive to be fired for each transported message?

The purpose of LineReceiver is to take a stream of bytes which no guarantees
about how many bytes will be received at a time and make it appear as a line
stream where each complete line is delivered separately.

Since you are using LineReceiver, rather than calling self.transport.write,
you should probably be using self.sendLine, which will insert the delimiter
bytes for you.  You must also be careful not to include the delimiter bytes
in the strings you pass to self.sendLine.

>    def lineReceived(self, line):
>        if self.app:
>            self.app.addMessage(line)
>    def connectionMade(self):
>        self.factory.clients.append(self)
>        host = self.transport.getPeer().host + ": joined the server."
>        for c in self.factory.clients:
>            c.transport.write(host + '\n')

So, consider doing this instead:


>            if self == c:
>                c.transport.write(self.getMOTD())   ### getMOTD just returns
>some string ###

As well as:


Also note that the default LineReceiver line delimiter is \r\n, not \n.


More information about the Twisted-Python mailing list