由于我的工作限制,我不得不依赖在 IIS 7.5 上托管我的webapp。 我已将 IIS 配置为通过 wfastcgi.py 为该应用程序提供服务。
问题是,单击登录按钮时,登录表单会在IIS上抛出 HTTP 500 错误。网站加载没有问题。此外,当我在烧瓶开发服务器上运行代码时,代码完全正常。
我有一个代码的扩展版本,可以访问sqlite数据库。 我能够将问题缩小到登录会话创建部分。 我不确定,问题是关于 IIS 如何使用 wfastcgi.py POST 和 GET 请求的方式>,或其他。
以下是代码的简化版本,它会抛出相同的错误: 更新:将视图函数更改为正确的返回重定向。
import wiw_ad_controller as parser
import sys
from flask import Flask, flash, render_template, redirect, url_for, request, session, abort
import os
app = Flask(__name__)
@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
if not session.get('logged_in'):
return render_template('login.html')
else:
return render_template('index.html')
@app.route('/login', methods=['POST'])
def login():
if request.form['username'] == 'admin' and request.form['password'] == 'admin':
session['logged_in'] = True
flash("Login was successfull")
else:
flash("Wrong password!")
return redirect(url_for('index'))
@app.route('/search', methods=['GET','POST'])
def lookup_data():
if (session.get('logged_in')==True):
if request.method == 'POST' or request.method == 'GET':
if (request.form['gpn'] and request.form['gpn'].isdigit()):
#code removed - not needed
return render_template('fetched_data.html', user_wiw=user_wiw, user_ad=user_ad)
else:
flash('Wrong ID')
return index()
return redirect(url_for('index'))
@app.route("/logout")
def logout():
session['logged_in'] = False
return redirect(url_for('index'))
@app.errorhandler(500)
def internal_error(exception):
app.logger.exception(exception)
return render_template('500.html'), 500
@app.errorhandler(404)
def internal_error(exception):
app.logger.exception(exception)
return render_template('404.html'), 404
if __name__ == '__main__':
app.secret_key=os.urandom(12)
app.run(debug=True)
答案 0 :(得分:4)
发现了这个问题。 我没有意识到,通过 wfastcgi.py 调用我的脚本, 主要功能永远不会运行。
登录功能无效,因为首先要运行app.secret_key=os.urandom(12)
。
我使用 Miguel Grinberg 的以下代码来找出问题所在,因为debug=True
未在 IIS prod环境中提供任何输出。
@app.errorhandler(500)
def internal_error(exception):
app.logger.exception(exception)
file_handler = RotatingFileHandler('C:\inetpub\wwwroot\logs.log', 'a', 1 * 1024 * 1024, 10)
file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'))
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('microblog startup')
return render_template('500.html'), 500
希望,这将有助于其他与我相同的情况。
注意:必须在wwwroot和所有文件上设置读/写权限才能使其正常工作。
答案 1 :(得分:0)
视图函数(search()
和login()
)的返回不正确。 E.g。
return redirect(url_for('index'))
应该用于返回索引页面。
请参阅有关重定向的Flask documentation。
答案 2 :(得分:0)
我有同样的错误,但我的应用已经有了一个密钥。
唉!经过两天的搜索,我用你的函数来显示回溯,我缩小了我的问题。
执行应用程序的进程无法访问数据库。此问题与iis的配置有关。应用程序池的标识必须能够访问数据库。您可以按照指示here
更改此身份对于任何人在使用Flask的iis中遇到的其他问题,我建议使用名称asp.net而不是python或flask来搜索它们,例如: 使用:“IIS内部服务器错误asp.net” 而不是:“IIS内部服务器错误瓶”
答案 3 :(得分:0)
伙计,我有同样的问题。这是适用于大多数人的解决方案。因为服务器不使用secret_key或其他东西连接到数据库,所以会发生此问题。
确保将以下行放在Flask应用程序的声明下方(在示例应用程序= Flask()中)
app.secret_key=os.urandom(12)
app.run(debug=True)
使用上述方法,您将可以轻松调试问题。