Skip to content

Instantly share code, notes, and snippets.

@ratulbasak
Last active February 17, 2018 20:56
Show Gist options
  • Save ratulbasak/8698c5b8ad1d479d0cbcfb4a18aa0321 to your computer and use it in GitHub Desktop.
Save ratulbasak/8698c5b8ad1d479d0cbcfb4a18aa0321 to your computer and use it in GitHub Desktop.
sysadmin flask api
from flask import Flask, jsonify
from flask import request, abort, make_response
import os, sys
app = Flask(__name__)
dir = "/"
@app.route('/api/', defaults={'req_path': ''})
@app.route('/api/<path:req_path>')
def get_tasks(req_path):
abs_path = os.path.join(dir, req_path)
if not os.path.exists(abs_path):
return abort(404)
if os.path.isfile(abs_path):
return send_file(abs_path)
files = os.listdir(abs_path)
total_size = 0
for dirpath, dirnames, filenames in os.walk(abs_path):
for f in filenames:
fp = os.path.join(dirpath, f)
st = os.stat(fp)
total_size += st.st_size
output = {'folders' : sorted(dirnames) , 'files': {'filename' : sorted(filenames), 'total_size' : total_size}}
return jsonify({'files and folders in /'+ req_path : output})
#return jsonifyI({"size" : total_size})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
click==6.7
Flask==0.12.2
itsdangerous==0.24
Jinja2==2.10
jsonify==0.5
MarkupSafe==1.0
scandir==1.7
Werkzeug==0.14.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment