Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Flask example #22

Merged
merged 3 commits into from
Oct 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions examples/flask/upload-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python3

from flask import Flask, request
import time
import os
import tempfile

from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import FileTarget

app = Flask(__name__)

page = '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form method=post enctype=multipart/form-data id="upload-file">
<input type=file name=file>
<input type=submit value=Upload>
</form><br>
'''


@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = FileTarget(os.path.join(tempfile.gettempdir(), "test"))

hdict = {}
for h in request.headers:
hdict[h[0]] = h[1]

parser = StreamingFormDataParser(headers=hdict)

parser.register('file', file)

timeA = time.perf_counter()
while True:
chunk = request.stream.read(8192)
if not chunk:
break
parser.data_received(chunk)
timeB = time.perf_counter()
print("time spent on file reception: %fs" % (timeB-timeA))
return file.multipart_filename + ": upload done"
return page


if __name__ == '__main__':
app.run(host='0.0.0.0')