简体   繁体   English

Python Flask装饰器和Apache mod_wsgi

[英]Python Flask Decorators and Apache mod_wsgi

I have created a fairly strait forward example of the Flask/Python application, and then I have decided to split code to the two files, so I have moved all decorators for Authentication to separate file, like following: 我已经创建了Flask / Python应用程序的一个相当棘手的示例,然后决定将代码拆分到两个文件中,因此我将所有用于身份验证的装饰器移动到了单独的文件中,如下所示:

#### test.py
from flask import Flask, request, abort, request, make_response, url_for, jsonify
from flask.ext.httpauth import HTTPBasicAuth
import pypyodbc, json, collections, _db, _sc
from customauth import CustomHTTPBasicAuth

app = Flask(__name__)
app.debug = True

from werkzeug.debug import DebuggedApplication
application = DebuggedApplication(app, evalex=True)

@app.errorhandler(400)
def not_found(error): return make_response(jsonify( { 'error': 'Bad request' } ), 400)
@app.errorhandler(404)
def not_found(error): return make_response(jsonify( { 'error': 'Not found' } ), 404)

auth = CustomHTTPBasicAuth()

@app.route('/hello')
@auth.login_required
def hello_world():
    name = request.args.get('name','')
    return 'Hello ' + name + '!'

if __name__ == '__main__':
    app.run()



#### customauth.py
from flask import Flask, jsonify, make_response, request
from flask.ext.httpauth import HTTPBasicAuth
import md5, socket

class CustomHTTPBasicAuth(HTTPBasicAuth):
    def __init__(self):
    super(CustomHTTPBasicAuth, self).__init__()

        @self.get_password
        def get_password(username):
            if (md5.new(username).hexdigest() == '0cc175b9c0f1b6a831c399e269772661'): #'0cc175b9c0f1b6a831c399e269772661' is md5('a')
                return '0cc175b9c0f1b6a831c399e269772661'
            return None 

        @self.hash_password
        def custom_authenticate(password):
            return '0cc175b9c0f1b6a831c399e269772661'

        @self.error_handler
        def unauthorized():
            return make_response(jsonify( { 'error': 'Unauthorized access' } ), 401)

#### wsgi.py
import sys
sys.path.insert(0, "c:/testflask/test")

from flask1 import app
application = app
  1. First issues is: this code works fine when executed with the python test.py Authentication form will popup asking for username and password, after entering 'a','a' authentication will pass and result 'Hello + whatever' will display. 第一个问题是:此代码与python test.py一起执行时可以正常工作。身份验证表单将弹出,要求输入用户名和密码,输入“ a”后,将通过“ a”身份验证,并显示“ Hello + what”。 But when executed through Apache mod_wsgi it does not run as expected authentication form will display unlimited number of times. 但是当通过Apache mod_wsgi执行时,它无法按预期的身份运行,身份验证表单将显示无数次。 Please can someone explain this code running under python and not under mod_wsgi, and is there a way to fix this? 请有人可以解释这段代码在python而不是mod_wsgi下运行,有没有办法解决? I am thinking that I am doing something wrong with the decorators but I am not sure what, also it does not answer why it's running via python and not via mod_wsgi. 我以为我在装饰器上做错了什么,但我不确定是什么,它也无法回答为什么它是通过python而不是通过mod_wsgi运行的。

  2. Another thing is how to get Debug page via apache mod_wsgi instead of "Internal Server Error" page? 另一件事是如何通过apache mod_wsgi而不是“内部服务器错误”页面来获取调试页面? I have tried with 'werkzeug.debug' and some other suggested things from the internet but nothing really worked for me. 我已经尝试过使用“ werkzeug.debug”和互联网上的其他一些建议,但是对我来说真的没有用。

Thanks, 谢谢,

#

I have found solution for the first bullet it is Apache settings only thing needed is to add WSGIPassAuthorization On in the config file. 我发现第一个项目符号是Apache设置的解决方案,唯一需要做的就是在配置文件中添加WSGIPassAuthorization On。

Still trying to find solution for debugging... 仍在尝试寻找调试解决方案...

The Flask debugger will likely not work if it detects that it is running in a multi process configuration as would often be the case for mod_wsgi if you are using embedded mode. 如果Flask调试器检测到它正在多进程配置中运行,则它可能无法工作,如果您使用的是嵌入式模式,mod_wsgi经常会遇到这种情况。 Ensure you are using daemon mode of mod_wsgi: 确保您使用的是mod_wsgi的守护程序模式:

WSGIDaemonProcess mysite
WSGIProcessGroup mysite

Note, do NOT set 'processes=1' to WSGIDaemonProcess. 请注意,请勿将“ processes = 1”设置为WSGIDaemonProcess。 Let it default to 1. Using 'processes' option with any value will cause wsgi.multiprocess to be True which is possibly what is causing the Flask debugger to be disabled. 将其默认设置为1。将'processes'选项与任何值一起使用将导致wsgi.multiprocess为True,这可能是导致Flask调试器被禁用的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM