How to get the authenticated user name under apache while in python code?
Question: Basically I just need to get the authenticated username from apache2, (for
basic / similar authentication schemes) but while in python.

In php, this is simple, and looks like this:

    
    
    $username = $_SERVER['PHP_AUTH_USER'];
    $password = $_SERVER['PHP_AUTH_PW'];`
    

Here is what the relevent section of the config in the site's sites-enabled
config looks like:

    
    
    #Provides Authentication for the admin pages
    <Location /admin>
      AuthType Basic
      AuthName "example.com"
      AuthUserFile /data/passwd_admin
      Require valid-user
    </Location>`
    

However, I cannot seem to find any method of retrieving this information from
python.


Answer: 
    from flask import request,Flask
    import json
    app = Flask(__name__)
    @app.route("/")
    def index():
        auth = request.authorization
        if not auth:return "No Auth!"
        return auth.username+":"+auth.password
    
    app.run(debug=True)
    

see : <http://flask.pocoo.org/snippets/8/>

