Skip to content

Commit

Permalink
improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhantgoel committed Nov 1, 2018
1 parent 5ee59a1 commit 31aa76d
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions examples/flask/upload-test.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,70 @@
#!/usr/bin/python3

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

from flask import Flask, request

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>
'''

page = dedent('''
<!doctype html>
<head>
<title>Upload new File</title>
</head>
<body>
<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>
</body>
''')


@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]
file_ = FileTarget(os.path.join(tempfile.gettempdir(), 'test'))

parser = StreamingFormDataParser(headers=hdict)
parser = StreamingFormDataParser(headers=request.headers)

parser.register('file', file_)

timeA = time.perf_counter()
time_start = 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))
time_finish = time.perf_counter()

response = dedent('''
<!doctype html>
<head>
<title>Done!</title>
</head>
<body>
<h1>
{file_name}: upload done
</h1>
<h2>
Time spent on file reception: {duration}s
</h2>
</body>
'''.format(file_name=file_.multipart_filename,
duration=(time_finish-time_start)))

return file_.multipart_filename + ": upload done"
return response
return page


Expand Down

0 comments on commit 31aa76d

Please sign in to comment.