【发布时间】:2013-09-21 16:48:24
【问题描述】:
我已经创建了一个相当严格的 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
第一个问题是:此代码在使用 python test.py 执行时可以正常工作 验证表单将弹出询问用户名和密码,输入'a'后,'a'验证将通过并显示结果'Hello + what'。 但是当通过 Apache mod_wsgi 执行时,它不会按预期运行身份验证表单将显示无限次。 请有人可以解释在 python 下而不是在 mod_wsgi 下运行的这段代码,有没有办法解决这个问题? 我在想我在装饰器上做错了什么,但我不确定是什么,它也没有回答为什么它是通过 python 而不是通过 mod_wsgi 运行的。
另一件事是如何通过 apache mod_wsgi 而不是“内部服务器错误”页面获取调试页面?我尝试过使用“werkzeug.debug”和其他一些来自互联网的建议,但没有什么对我真正有用。
谢谢,
#
我已经找到了第一个项目符号的解决方案,它是 Apache 设置,唯一需要的就是在配置文件中添加 WSGIPassAuthorization On。
仍在寻找调试解决方案...
【问题讨论】:
-
我认为它不能解决您的问题,但您已经定义了函数
not_found两次,一次用于 404,一次用于 400。 -
另外,请将您的每个问题分成单独的问题。
-
有趣的 400, 404 使用相同的名称可以正常工作,可能是因为它被装饰器包裹,因此被认为是匿名函数我已经尝试了一种或另一种方法,它工作正常。
标签: python apache flask decorator mod-wsgi