N
N
nivs2014-10-22 01:47:13
Python
nivs, 2014-10-22 01:47:13

Is it possible in Python 3 to read JSON objects from a socket?

JSON objects are sent over the network from time to time. Is there a specialized solution to constantly read data from the socket, and when one complete JSON arrives, send it for processing, then immediately wait for the next JSON object to also send it for processing, and so on? The socket should not be broken, objects will arrive from time to time one by one or in batches. Ideally, receive data as after json.loads() .

I tried socket.makefile() together with ijson - nothing good came out, cpiekarski.com/2011/05/09/super-easy-python-json-c... written for the 2nd and did not work out in the 3rd.

It should work on both Linux and Windows.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ruchkin, 2014-10-23
@nivs

If it is possible to influence exactly how JSON arrives, then you can ask to separate them, for example, by a line break. A line feed cannot occur in [not pretty] JSON itself, it will be escaped in strings, and therefore you can increase the data read from the socket until a line feed is encountered, and what is before it, feed json.loads, and the rest continue to grow.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question