Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """Basic authentication example
  3. This example demonstrates how to protect Flask endpoints with basic
  4. authentication, using secure hashed passwords.
  5. After running this example, visit http://localhost:5000 in your browser. To
  6. gain access, you can use (username=john, password=hello) or
  7. (username=susan, password=bye).
  8. """
  9. from flask import Flask
  10. from flask_httpauth import HTTPBasicAuth
  11. from werkzeug.security import generate_password_hash, check_password_hash
  12.  
  13.  
  14.  
  15. app = Flask(__name__)
  16. auth = HTTPBasicAuth()
  17.  
  18. users = {
  19. "john": generate_password_hash("hello"),
  20. "susan": generate_password_hash("bye")
  21. }
  22.  
  23.  
  24. @auth.verify_password
  25. def verify_password(username, password):
  26. if username in users:
  27. return check_password_hash(users.get(username), password)
  28. return False
  29.  
  30.  
  31. @app.route('/')
  32. @auth.login_required
  33. def index():
  34. return "Hello, %s!" % auth.username()
  35.  
  36.  
  37. x = 0
  38. @app.route('/hello')
  39. @auth.login_required
  40. def get_account():
  41. global x
  42. x += 1
  43. return 'Account %s \n' % x
  44.  
  45.  
  46. if __name__ == '__main__':
  47. x = 0
  48. app.run(host= 'type your ip here')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement