Setting up python socket server with non-standard termination string.

Jean-Paul Calderone exarkun at divmod.com
Tue May 8 14:44:49 EDT 2007


On 8 May 2007 06:59:26 -0700, indy1000 at gmail.com wrote:
>Hi,
>
>I'm trying to create a tcp/ip port listening socket server for various
>cellular devices that don't use the standard "\r\n" termination string
>at the end of their message.  Instead, they use "-ND-".  Do you know
>how I can setup such a server, preferably using either 'socket.py',
>'serversocket.py' or 'twisted.py'?

You can use the LineReceiver (or LineOnlyReceiver) from Twisted to do this
quite easily:

    from twisted.internet import reactor
    from twisted.internet.protocol import ServerFactory
    from twisted.protocols.basic import LineOnlyReceiver

    class CellProtocol(LineOnlyReceiver):
        delimiter = '-ND-'

    def lineReceived(self, line):
        print 'got a line'

    f = ServerFactory()
    f.protocol = CellProtocol
    reactor.listenTCP(12345, f)
    reactor.run()

Jean-Paul



More information about the Python-list mailing list