-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from patrislav1/master
added Flask example
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |