Skip to content

Instantly share code, notes, and snippets.

@pbzweihander
Last active June 5, 2020 10:17
Show Gist options
  • Save pbzweihander/032d1f28373b66461b3635faa1997350 to your computer and use it in GitHub Desktop.
Save pbzweihander/032d1f28373b66461b3635faa1997350 to your computer and use it in GitHub Desktop.
Flask Streaming Example
from requests import get
URL = 'http://localhost:13000/'
def main():
resp = get(URL, stream=True)
for b in resp.iter_content(decode_unicode=True):
print(b, end='')
print()
if __name__ == '__main__':
main()
import time
from flask import Flask, Response
def stream():
try:
i = 0
while True:
print('sending hello', i)
yield 'Hello {}!\n'.format(i)
i += 1
if i > 5:
break
time.sleep(5)
finally:
print('end sending hello')
def main():
app = Flask(__name__)
@app.route('/')
def streaming_api():
return Response(stream())
app.run('0.0.0.0', 13000)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment