Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. const req_url = 'http://localhost:5000/'
  2. let resp = fetch(req_url, {
  3. method: 'POST',
  4. mode: 'no-cors',
  5. headers: new Headers({
  6. 'Authorization' : 'Basic dXNlcjpwd2Q=', // user:pwd
  7. }),
  8. }).then((resp) => resp)
  9.  
  10. // или так
  11. let headers = new Headers()
  12. headers.append('Authorization', 'Basic Y2lhdDpwd2Q=')
  13. let resp = fetch(req_url, {
  14. method: 'POST',
  15. mode: 'no-cors',
  16. headers: headers,
  17. credentials: 'include'
  18. }).then((resp) => resp)
  19.  
  20. // или даже так
  21. let headers = new Headers()
  22. headers.append('Authorization', 'Basic Y2lhdDpwd2Q=')
  23. let resp = fetch(req_url, {
  24. method: 'POST',
  25. mode: 'no-cors',
  26. headers: headers,
  27. credentials: 'include'
  28. }).then((resp) => resp)
  29.  
  30. #!/usr/bin/env python3
  31. from functools import wraps
  32. from flask import Flask, request, Response
  33.  
  34. app = Flask(__name__)
  35.  
  36. def check_auth(username, password):
  37. return username == 'user' and password == 'pwd'
  38.  
  39. def authenticate():
  40. return Response(
  41. 'Could not verify your access level for that URL.n'
  42. 'You have to login with proper credentials', 401,
  43. {'WWW-Authenticate': 'Basic realm="Login Required"'})
  44.  
  45. def requires_auth(f):
  46. @wraps(f)
  47. def decorated(*args, **kwargs):
  48. auth = request.authorization
  49. if not auth or not check_auth(auth.username, auth.password):
  50. return authenticate()
  51. return f(*args, **kwargs)
  52. return decorated
  53.  
  54. @app.route('/', methods=['POST', 'GET'])
  55. @requires_auth
  56. def secret_page():
  57. return """
  58. <!doctype HTML>
  59. <html>
  60. <head></head>
  61. <body><h1>Success!</h1></body>
  62. </html>
  63. """
  64.  
  65.  
  66. if __name__ == '__main__':
  67. app.run(debug=True)
  68.  
  69. const req_url = 'http://localhost:5000/'
  70. let resp = fetch(req_url, {
  71. method: 'POST',
  72. mode: 'no-cors',
  73. headers: {
  74. 'Authorization' : 'Basic dXNlcjpwd2Q=',
  75. },
  76.  
  77. }).then((resp) => resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement