-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.py
94 lines (79 loc) · 3.17 KB
/
server.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# simple file uploader and downloader service
import os
import urllib
import cgi
from flask import Flask, request, redirect, url_for, send_from_directory
from secure_file import secure_filename
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
BASE_DIR = os.path.dirname(__file__)
print ("dir:%s" % BASE_DIR)
UPLOAD_FOLDER = 'files'
ALLOWED_EXTENSIONS = set(['txt','jpg','pdf','apk','ppt','doc','png','img','tgz','zip','tar','bz2'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
def list_directory(path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except os.error:
return "No permission to list directory"
list.sort(key=lambda a: a.lower())
f = StringIO()
displaypath = cgi.escape(urllib.unquote(path))
f.write("<body><h2>Directory listing for %s</h2>" % displaypath)
f.write("<form ENCTYPE=\"multipart/form-data\" method=\"post\">")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write('<li><a href="uploads/%s">%s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
#self.send_response(200)
#self.send_header("Content-type", "text/html")
#self.send_header("Content-Length", str(length))
#self.end_headers()
return f
@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
uploads = os.path.join(BASE_DIR, UPLOAD_FOLDER)
return send_from_directory(directory=uploads, filename=filename)
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
print('filename:%s' % file.filename)
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('index'))
return """
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>
%s
""" % "<br>".join(list_directory(app.config['UPLOAD_FOLDER']))
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5001, debug=True)