-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.py
38 lines (35 loc) · 1.05 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from bottle import route, run, request, response
@route('/', method='GET')
def do_form():
form = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Upload Test</title>
</head>
<body>
<form action="/upload" method="post"
enctype="multipart/form-data">
<textarea name=meta rows=10 cols=60>{"name":"test.png"}</textarea><br/>
<input type="file" name="data"/>
<input type="submit" value="POST data" />
</form>
</body>
</html>
"""
response.content_type = 'text/html; charset=utf-8'
return form
@route('/upload', method='POST')
def do_upload():
meta = request.forms.meta
data = request.files.get("data")
if data and data.file:
raw = data.file.read()
filename = data.filename
with open("/tmp/%s" % filename, 'w+') as open_file:
open_file.write(raw)
return "<b>Metadata:</b><br/>%s<hr/>You uploaded <b>%s</b> (<b>%d</b> bytes)." \
% (meta, filename, len(raw))
return "You missed a field."
run(host='0.0.0.0', port=8080, debug=True, reloader=True)