Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import os.path
  2. from functools import wraps
  3. from flask import Flask, request, Response, redirect
  4. from flask_autoindex import AutoIndex
  5.  
  6. fileroot = os.path.join(os.path.realpath(os.path.curdir), 'files')
  7. app = Flask(__name__)
  8. idx = AutoIndex(app,
  9. browse_root=fileroot,
  10. add_url_rules=False)
  11.  
  12. def check_auth(username, password):
  13. """ This function is called to check if a username / password combination is
  14. valid.
  15. """
  16. return username == 'USERNAME' and password == 'PASSWORD'
  17.  
  18. def authenticate():
  19. """ Sends a 401 response that enables basic auth """
  20. return Response(
  21. 'Could not verify your access level for that URL.\n'
  22. 'You have to login with proper credentials', 401,
  23. {'WWW-Authenticate': 'Basic realm="Auth"'})
  24.  
  25. def requires_auth(f):
  26. @wraps(f)
  27. def decorated(*args, **kwargs):
  28. auth = request.authorization
  29. if not auth or not check_auth(auth.username, auth.password):
  30. return authenticate()
  31. return f(*args, **kwargs)
  32. return decorated
  33.  
  34. @app.route('/')
  35. @app.route('/<path:path>')
  36. @requires_auth
  37. def autoindex(path='.'):
  38. # For Windows
  39. relpath = os.path.join(*path.split('%5C'))
  40. normpath = os.path.normpath(relpath)
  41. if normpath.startswith(os.path.pardir):
  42. normpath = '/'
  43. if normpath != path:
  44. if normpath == '.':
  45. return redirect('/')
  46. return redirect(normpath)
  47. return idx.render_autoindex(normpath)
  48.  
  49. if __name__ == '__main__':
  50. app.run(threaded=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement