Last active
March 23, 2017 11:45
Flask HTTPBasicAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask import Flask, request, abort | |
app = Flask(__name__) | |
@app.route('/') | |
def index(): | |
return 'Hello World' | |
@app.before_request | |
def request_auth(): | |
auth = request.authorization | |
if not (auth and auth.username == 'xyz' and auth.password == '123'): | |
abort(401) | |
@app.errorhandler(401) | |
def unauthorized(error): | |
resp = error.get_response() | |
resp.headers['WWW-Authenticate'] = 'Basic realm="Restricted Content"' | |
# resp.headers['WWW-Authenticate'] = 'Basic realm="Login Required"' | |
return resp | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import requests | |
url = 'http://127.0.0.1:5000/' | |
r = requests.get(url, auth=('xyz', '12')) | |
print(r.status_code) | |
print(r.headers) | |
print(r.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment