1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
from __future__ import print_function
import gc
import io
import os.path
import time
import flask
import flask.helpers
application = flask.Flask(__name__)
FILENAME = os.path.join(os.path.dirname(__file__), "static", "test.txt")
FILENAME2 = os.path.join(os.path.dirname(__file__), "static", "test2.txt")
@application.after_request
def _after(response):
gc.collect()
fds = os.listdir("/proc/self/fd")
print("PY: objects:", len(gc.get_objects()), "fds:", len(fds))
return response
@application.route("/")
def index():
return "HELLO\n"
@application.route("/1")
def send_file_1():
fp = open(FILENAME, "rb")
return flask.send_file(fp, attachment_filename="test.txt")
@application.route("/2")
def send_file_2():
bio = io.BytesIO(b"cookie\n")
return flask.send_file(bio, attachment_filename="test.txt")
@application.route("/3")
def send_file_3():
"""
What happens if we call the wsgi.file_wrapper twice?
This should respond with cookie2
"""
fp = open(FILENAME, "rb")
flask.send_file(fp, attachment_filename="test.txt")
fp = open(FILENAME2, "rb")
return flask.send_file(fp, attachment_filename="test.txt")
@application.route("/4")
def send_file_4():
"""
Non-filelike object to send_file/wrap_file/wsgi.file_wrapper.
AttributeError on the call to wsgi.file_wrapper.
"""
return flask.send_file(object(), attachment_filename="test.txt")
@application.route("/stream1")
def stream1():
"""
Unrelated to wsgi.file_wrapper, just ensuring the iterator stuff still works.
"""
def _yield():
start = time.time()
for i in range(3):
time.sleep(0.1)
yield " {:.2f} cookie".format(time.time() - start).encode("utf-8")
yield b"\n"
return flask.Response(_yield(), mimetype="text/plain")
@application.route("/stream2")
def stream2():
"""
Yielding the result of a wrap_file call with a file object.
gunicorn / werkzeug do not support this as it's not required.
"""
fp = open(FILENAME, "rb")
resp = flask.helpers.wrap_file(flask.request.environ, fp)
print("PY: resp after return", hex(id(resp)))
def _yield():
print("PY: _yield() run", hex(id(resp)), repr(resp))
yield resp
return flask.Response(_yield())
@application.route("/stream3")
def stream3():
"""
Yielding the result of a wrap_file call with a BytesIO object.
gunicorn / werkzeug do not support this as it's not required.
"""
bio = io.BytesIO(b"cookie\n")
resp = flask.helpers.wrap_file(flask.request.environ, bio)
def _yield():
yield resp
return flask.Response(_yield())
@application.route("/stream4")
def stream4():
"""
werkzeug logs: AssertionError: applications must write bytes
gunicorn logs: TypeError: <Response streamed [200 OK]> is not a byte
uwsgi didn't log, should now..
"""
fp = open(FILENAME, "rb")
resp = flask.send_file(fp, attachment_filename="test.txt")
print("PY: resp after return", hex(id(resp)))
def _yield():
print("PY: _yield() run", hex(id(resp)), repr(resp))
yield resp
return flask.Response(_yield(), direct_passthrough=True)
|