pythonsocketstcptelnettelnetlib

Parsing raw TCP data line by line in Python


I'm getting stock market data via TCP. I'm trying to connect with Python to the broker, e.g. via Telnet (telnetlib) with read_eager() or via socket with socket.recv().

This is how the output looks like:

DE000XX6XP73;0.0000;2.44;G;2020-04-24;18:22:04.200'
';\r\nDE000XX9JX82;0.0000;0.0;G;2020-04-24;18:22:06.2'
'18;\r\nDE000XX9XXW3;0.0000;127.17;G;2020-04-24;18:22'
':21.954;\r\nDE000XX68XV1;0.0000;3.22;G;2020-04-24;18'
':21:11.517;\r\nDE000XX3H0X0;0.0000;1.81;G;2020-04-24'

and so on.

As you can see, this is one line: DE000XX6XP73;0.0000;2.44;G;2020-04-24;18:22:04.200;

How can I parse each line? The TCP data stream never ends - it has no end of file and I need to process it in "real-time". The problem is that I'm getting "broken lines" most of the time, e.g. DE000XX3H0X0;0.0000;1.81;G;2020-04-24


Solution

  • I managed to find an own solution in Python which wasn't working as I expected. (Sync) Python was way too slow to handle those requests in real-time, it somehow stopped working after some time. I switched to async JS which is now working flawlessly.