-
-
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 #9 from kolomenkin/r_examples
add bottle example
- Loading branch information
Showing
8 changed files
with
83 additions
and
3 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,2 @@ | ||
bottle==0.12.13 | ||
paste==2.0.3 |
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,47 @@ | ||
import os.path | ||
import tempfile | ||
from time import time | ||
|
||
import bottle | ||
|
||
from streaming_form_data import StreamingFormDataParser | ||
from streaming_form_data.targets import ValueTarget, FileTarget | ||
|
||
|
||
bottle.TEMPLATE_PATH = [os.path.join(os.path.dirname(__file__), 'templates')] | ||
|
||
|
||
@bottle.route('/') | ||
@bottle.view('index.html') | ||
def root_page(): | ||
return {} | ||
|
||
|
||
@bottle.post('/upload') | ||
@bottle.view('upload.html') | ||
def upload_page(): | ||
value = ValueTarget() | ||
name = 'uploaded-file-tornado-{}.dat'.format(int(time())) | ||
file = FileTarget(os.path.join(tempfile.gettempdir(), name)) | ||
|
||
parser = StreamingFormDataParser(headers=bottle.request.headers) | ||
|
||
parser.register('name', value) | ||
parser.register('file', file) | ||
|
||
while True: | ||
chunk = bottle.request.environ['wsgi.input'].read(8192) | ||
if not chunk: | ||
break | ||
parser.data_received(chunk) | ||
|
||
return {'name': value.value, | ||
'filename': file.filename} | ||
|
||
|
||
if __name__ == '__main__': | ||
bottle.run(app=bottle.app(), | ||
server='paste', | ||
host='localhost', | ||
port=9000, | ||
debug=True) |
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,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Upload something!</title> | ||
</head> | ||
<body> | ||
<form action="/upload" method="post" enctype="multipart/form-data"> | ||
|
||
<input type="text" name="name"> | ||
<input type="file" name="file"> | ||
|
||
<input type="submit" value="Submit"> | ||
</form> | ||
</body> | ||
</html> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Done!</title> | ||
</head> | ||
<body> | ||
<p>Name: {{ name }}</p> | ||
<p>filename: {{ filename }}</p> | ||
|
||
<a href="/">Do it again</a> | ||
</body> | ||
</html> |