Skip to content

Instantly share code, notes, and snippets.

@DasIch
Created June 13, 2013 18:54
Show Gist options
  • Save DasIch/5776338 to your computer and use it in GitHub Desktop.
Save DasIch/5776338 to your computer and use it in GitHub Desktop.
from flask import Flask, stream_with_context, json, Response
app = Flask(__name__)
@app.route('/')
def index():
def gen():
yield json.dumps({'foo': 'spam'})
yield json.dumps({'bar': 'eggs'})
return Response(stream_with_context(gen()), content_type='application/json')
if __name__ == '__main__':
app.run(debug=True)
% curl -v 127.0.0.1:5000
* About to connect() to 127.0.0.1 port 5000 (#0)
* Trying 127.0.0.1...
* connected
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5
> Host: 127.0.0.1:5000
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: application/json
< Connection: close
< Server: Werkzeug/0.9 Python/2.7.3
< Date: Thu, 13 Jun 2013 18:52:43 GMT
<
* Closing connection #0
{"foo": "spam"}{"bar": "eggs"}%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment