1

I really need some simple example to demonstrate how to receive streaming data sent from remote side.

I spent several hours online to search example code, but all I found is to sent streaming data to remote side, not receive from remote side.

The stream data is in JSON format, looks like:

15:15
15:17
15:20
15:21
15:22 

I would greatly appreciate someone who could provide me some simple, "hello-world like", examples which could receive such data. I am very new to flask.

1
  • The web application shouldn't be the handler for incoming streaming content. Commented Feb 7, 2015 at 8:39

1 Answer 1

1

Web applications are usually designed for sending data to clients not being clients themselves. (AKA, the client has to initiate the connection, not the application).

Although, your question is a bit vague about how the stream is opened, and who should initiate the calls, here's a small example, that might get you started.

Simple listener that streams the incoming data. Here's an example using python-requests:

from contextlib import closing
import requests
with closing(requests.get('http://httpbin.org/get', stream=True)) as r:
    while r.iter_content:
        # Do something with the stream.. 
        pass
Sign up to request clarification or add additional context in comments.

3 Comments

I have another short question. I simply add ' print r ' right after # do something#, and it returns <response [200]> . So this message is actually come from remote side, am I right? I want to build another response for testing my program, how can I do that? Thanks very much
Nope, this is just a 'nice' way the response object is printed out (aka the __repr__ property). You can read the response data from either r.text, r,json or r.raw. See here: docs.python-requests.org/en/latest/user/quickstart/…
How to do same using flask test_client get method?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.