get a line of text from a socket...

Jean-Paul Calderone exarkun at divmod.com
Fri Aug 25 13:32:55 EDT 2006


On 25 Aug 2006 09:37:09 -0700, KraftDiner <bobrien18 at yahoo.com> wrote:
>If you don't know how long your input data is going to be how can you
>at least treat it a text line at a time... like looking for new line in
>the data... Right now recv blocks.   Yes I could do a select, but the
>examples seem a bit complicated for a simple line oriented input...

Here's a simple line-based echo implementation using Twisted:

    from twisted.internet import reactor, protocol
    from twisted.protocols import basic

    class LineEcho(basic.LineReceiver):
        def lineReceived(self, line):
            print 'Received line:', repr(line)

    factory = protocol.ServerFactory()
    factory.protocol = LineEcho
    reactor.listenTCP(12345, factory)
    reactor.run()

Jean-Paul



More information about the Python-list mailing list