Skip to content

Commit

Permalink
Merge pull request #9 from kolomenkin/r_examples
Browse files Browse the repository at this point in the history
add bottle example
  • Loading branch information
siddhantgoel committed May 19, 2018
2 parents eb295af + 7e52b26 commit 24d6d94
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 3 deletions.
2 changes: 2 additions & 0 deletions examples/bottle/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bottle==0.12.13
paste==2.0.3
47 changes: 47 additions & 0 deletions examples/bottle/stream_request_body.py
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)
16 changes: 16 additions & 0 deletions examples/bottle/templates/index.html
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.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os.path
import tempfile
from time import time

from tornado.web import Application, RequestHandler, stream_request_body
Expand All @@ -12,7 +13,8 @@
class UploadHandler(RequestHandler):
def prepare(self):
self.value = ValueTarget()
self.file_ = FileTarget('/tmp/file-{}.dat'.format(int(time())))
name = 'uploaded-file-tornado-{}.dat'.format(int(time()))
self.file_ = FileTarget(os.path.join(tempfile.gettempdir(), name))

self._parser = StreamingFormDataParser(headers=self.request.headers)

Expand Down Expand Up @@ -44,11 +46,11 @@ def main():
)

app = Application(handlers, **settings)
app.listen(9999)
app.listen(9999, address='localhost')

IOLoop().current().start()


if __name__ == '__main__':
print('Listening on port 9999')
print('Listening on localhost:9999')
main()
13 changes: 13 additions & 0 deletions examples/tornado/upload.html
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>

0 comments on commit 24d6d94

Please sign in to comment.