Search code examples
pythonsocketsnetwork-programmingprotocolstwisted

What is the difference between twisted.protocols.basic.LineReceiver and twisted.internet.protocol.Protocol?


For a typical client-server based application, how do the two differ from each other. Specifically, what is special with a line-based protocol? Even better, when does a class have to inherit from Protocol and when from LineReceiver?


Solution

  • Difference becomes by received data handling.

    Protocol have dataReceived function. It will be called whenever data received.

    LineReceiver overrides Protocol. It is implements a basic messaging format that messages separated with ' \r\n'.

    Let's assume server writes messages like;

    request.write("Lorem ipsum")
    request.write("do amet siempre.\r\n")
    request.write("We have Drogba!\r\n")
    

    Messaged received on the Client side with implements Protocol;

    def dataReceived(self, data):
       print data
    .
    .
    output:
    
    Lorem ipsum
    do amet siempre.
    We have Drogba!
    

    Messaged received on the Client side with implements LineReceiver;

    def lineReceived(self, line):
       print line
    .
    .
    output: 
    Lorem ipsum do amet siempre.
    We have Drogba!
    

    I hope it is helpful. For more information you can look reference or comment to ask.