I have set up locally Redis and I want to connect to a remote server that provides a synthetic data stream in the form < ID, value >. I have so far managed to connect to the server mentioned above using sockets, read the data stream and just print it. Instead I want to store the pairs in a hash data structure (I'm going to store more informations about each ID later). The problem is that I don't know how to parse the data stream in order to use hget
and how to it continuously. In a higher level I would like to be able to pass Name and Value from the incoming data stream as arguments to hget
. Forgot to mention I'm using Python API.
So far:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('xx.xx.xx.xxx', 1337))
while 1:
data = client_socket.recv(512)
print data
A sample of the data stream:
'AMZN,780.6758\n'
'TSLA,197.1802\n'
'CSCO,29.7491\n'
'GOOG,761.3758\n'
'AAPL,112.4122\n'
'GRPN,4.5848\n'
'FB,121.1232\n'
'MSFT,60.3529\n'
'INTC,35.9056\n'
'NVDA,94.473\n'
'QCOM,68.7389\n'
'AMZN,780.6761\n'
'TSLA,197.1798\n'
'CSCO,29.7486\n'
'GOOG,761.3755\n'
'AAPL,112.4122\n'
'GRPN,4.5848\n'
'FB,121.1237\n'
'MSFT,60.353\n'
'INTC,35.9054\n'
'NVDA,94.473\n'
'QCOM,68.7391\n'
I'm not sure if there is a guarantee that all lines are fully formatted but let's that they are.
Parsing a single non-empty line into a key/value pair is as simple as :
Assuming your data may be incomplete (unterminated record) and that it's the linefeed that marks the end of a record, you can store incomplete records in a buffer and add them back before parsing, so your function might look something like this:
The implementation of
do_something_with(k, v)
is left as an exercise to the reader.