reading from sys.stdin without newline or EOF

Павел Тявин picture Павел Тявин · Sep 6, 2012 · Viewed 20k times · Source

I want to recieve data from my gps-tracker. It sends data by tcp, so I use xinetd to listen some tcp port and python script to handle data. This is xinetd config:

service gps-gprs
{
    disable     = no
    flags       = REUSE
    socket_type = stream
    protocol    = tcp
    port    = 57003
    user        = root
    wait        = no
    server      = /path/to/gps.py
    server_args     = 3
} 

Config in /etc/services

gps-gprs        57003/tcp           # Tracking system

And Python script gps.py

#!/usr/bin/python
import sys

def main():
    data = sys.stdin.readline().strip() 
    #do something with data
    print 'ok'

if __name__ =='__main__':
    main()

The tracker sends data strings in raw text like

$GPRMC,132017.000,A,8251.5039,N,01040.0065,E,0.00,,010111,0,,A*75+79161234567#

The problem is that sys.stdin in python script doesn't recieve end of line or end of file character and sys.stdin.readline() goes forever. I tried to send data from another pc with a python script

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('', 57003))
s.sendall( u'hello'  )
data = s.recv(4024)
s.close()
print 'Received', data

and if the message is 'hello', it fails, but if the message is 'hello\n', it's ok and everything is fine. But I don't know ho to tell tracker or xinetd to add this '\n' at the end of messages. How can I read the data from sys.stdin without EOF or EOL in it?

Answer

aDoN picture aDoN · Jun 17, 2015

Simple:

data=sys.stdin.read().splitlines()

for i in data:
        print i

No newlines