jean
jean

Reputation: 251

self.sendLine or self.transport.write?

In twisted, what is the difference between calling self.transport.write () and self.sendLine () ? For example, the following program behaves identically whatever I call in the lineReceived method :

class FooProtocol(basic.LineReceiver):

    delimiter = '\n'

    def connectionMade(self):
        self.sendLine("Foo")

    def lineReceived(self, line):
        self.sendLine("Echoed: " + line)
        #self.transport.write("Echoed: " + line + "\n")

if __name__ == "__main__":
    stdio.StandardIO(FooProtocol())
    reactor.run()

Is there a more pythonic (or twistedic ...) way of doing this ?

thanks in advance !

Upvotes: 1

Views: 2075

Answers (1)

jfs
jfs

Reputation: 414395

sendLine() is a convience method. The default implementation is:

def sendLine(self, line):
    return self.transport.write(line + self.delimiter)

sendLine() is a slightly higher-level function. You don't need to use self.transport.write() directly in a line-oriented protocol.

Upvotes: 5

Related Questions