I am receiving line by line streaming data from a device with my Python program. Data looks like this:
MSG,4,0,0,80068C,0,2015/01/22,14:40:15.815,2015/01/22,14:40:15.815,,,228.9,212.5,,,-1280,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:28.722,2015/01/22,14:40:28.722,,,230.5,212.2,,,-1920,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:34.284,2015/01/22,14:40:34.284,,,,,,,,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:35.144,2015/01/22,14:40:35.144,,,,,,,,,,,,0
MSG,5,0,0,80068C,0,2015/01/22,14:40:35.144,2015/01/22,14:40:35.144,,5400,,,,,,,,,0,0
MSG,3,0,0,80068C,0,2015/01/22,14:40:36.065,2015/01/22,14:40:36.065,,5375,,,,,,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:36.644,2015/01/22,14:40:36.644,,,231.6,211.7,,,-1728,,,,,0
MSG,8,0,0,80068C,0,2015/01/22,14:40:37.503,2015/01/22,14:40:37.503,,,,,,,,,,,,0
MSG,4,0,0,80068C,0,2015/01/22,14:40:38.815,2015/01/22,14:40:38.815,,,231.6,211.7,,,-1408,,,,,0
Now I want to connect to SQLite database from the program and save each line to the database in CSV format at the instant they are received.
My Python code is given below:
from socket import *
HOST = 'localhost'
PORT = 30003 #our port from before
ADDR = (HOST,PORT)
BUFSIZE = 4096
sock = socket( AF_INET,SOCK_STREAM)
sock.connect((ADDR))
def readlines(sock, recv_buffer=4096, delim='\n'):
buffer = ''
data = True
while data:
data = sock.recv(recv_buffer)
buffer += data
while buffer.find(delim) != -1:
line, buffer = buffer.split('\n', 1)
yield line.strip('\r\n')
return
for line in readlines(sock):
print line