Skip to content

Commit

Permalink
Merge pull request #22 from patrislav1/master
Browse files Browse the repository at this point in the history
added Flask example
  • Loading branch information
siddhantgoel committed Oct 13, 2018
2 parents b12197b + c9a5f5a commit ebe3ec6
Showing 1 changed file with 50 additions and 0 deletions.
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')

0 comments on commit ebe3ec6

Please sign in to comment.