-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
56 lines (44 loc) · 1.63 KB
/
server.py
File metadata and controls
56 lines (44 loc) · 1.63 KB
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
import datetime
import os
import flask
from flask import abort, jsonify, send_from_directory
from natsort import natsorted
from utils import logger
app = flask.Flask(__name__)
result_dir = './results'
result_dir = os.path.abspath(result_dir)
@app.route('/')
def main():
return jsonify({'msg': 'server running.'})
@app.route('/workdirs')
def workdirs():
workdirs = os.listdir(os.path.join(result_dir))
workdirs = natsorted(workdirs, reverse=False)
return jsonify(list(map(lambda x: f'zterp/{x}', workdirs)))
@app.route('/res', defaults={'path': ''})
@app.route('/res/<path:path>', methods=['GET'])
def res(path):
abs_path = os.path.join(result_dir, path)
# logger.debug(abs_path)
if not os.path.exists(abs_path) or not abs_path.startswith(os.path.abspath(result_dir)):
return abort(404)
if os.path.isdir(abs_path):
items = os.listdir(abs_path)
items = natsorted(items, reverse=True)
html = f'''
<h2>Files under directory {abs_path}:</h2>
<ul>
'''
for item in items:
mod_date = os.path.getmtime(os.path.join(abs_path, item))
readable_mod = datetime.datetime.fromtimestamp(mod_date).strftime('%Y-%m-%d %H:%M:%S')
item_path = os.path.join(path, item)
html += f'''<li>
<a href="/res/{item_path}">{item}</a> <span style="color: #888888; font-size: 0.8rem; user-select: none;">Mod: {readable_mod}</span>
</li>'''
html += '</ul>'
return html
# Serve file
return send_from_directory(result_dir, path)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5275, debug=True)