Flask部署Azure App Service点击提交出现500内部服务器错误求助
Flask应用部署Azure App Service后提交文件出现500错误排查建议
问题背景
本地虚拟环境用flask run运行正常,功能为接收上传文件与static文件夹内的abc.xlsx比对后返回Excel结果,但部署到Azure App Service后,点击提交按钮触发500内部服务器错误:
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
用户提供的代码
from operator import index from flask import Flask, render_template, send_file, make_response from flask_wtf import FlaskForm from wtforms import FileField, SubmitField from werkzeug.utils import secure_filename import os import io import pandas as pd from wtforms.validators import InputRequired app = Flask(__name__) app.config['SECRET_KEY'] = 'supersecretkey' app.config['UPLOAD_FOLDER'] = 'static/files' class UploadFileForm(FlaskForm): file = FileField("File", validators=[InputRequired()]) submit = SubmitField("Submit") @app.route('/', methods=['GET',"POST"]) @app.route('/home', methods=['GET',"POST"]) def home(): form = UploadFileForm() if form.validate_on_submit(): file = form.file.data # First grab the file prop_file = os.path.join('static/files','abc.xlsx') df1 = pd.read_excel(prop_file) df_Check = pd.read_excel(file) # format assoc to remove any special characters df_Check['New_Assoc_No'] = df_Check.ASSOCIATION_NO.str.replace('\\W', '') df1['New_Assoc_No'] = df1.ASSOCIATION_NO.str.replace('\\W', '') # format name to remove any special characters df_Check['NewName'] = df_Check.NAME.str.replace('\\W', '') df1['NewName'] = df1.NAME.str.replace('\\W', '') df_dup_assoc = pd.merge(df_Check,df1, on="New_Assoc_No", how="inner") df_dup_name = pd.merge(df_Check,df1, on="NewName", how="inner") out = io.BytesIO() with pd.ExcelWriter(out, engine="xlsxwriter") as writer: df_dup_assoc.to_excel(writer, index=False, sheet_name= "Assoc") df_dup_name.to_excel(writer, index=False, sheet_name= "Name") writer.save writer.close out.seek(0) #display result on a new page return send_file(out, attachment_filename="dup.xlsx", as_attachment=True) return render_template('index.html', form=form) if __name__ == '__main__': app.run(debug=True)
排查与修复建议
- 开启Azure详细错误日志:在Azure门户的应用服务控制台,进入「监视」>「日志流」,或开启「应用程序日志」(文件系统或Blob存储),获取具体的报错堆栈信息,这是定位500错误的核心步骤。
- 修正文件路径问题:本地相对路径在Azure环境中可能失效,改用
app.root_path构建绝对路径,确保能找到static下的abc.xlsx:prop_file = os.path.join(app.root_path, 'static', 'files', 'abc.xlsx') - 检查依赖包完整性:确保
requirements.txt包含所有必要依赖,避免Azure部署时遗漏:
注:pandas读取Excel可能依赖openpyxl,本地已安装但Azure环境可能缺失。flask flask-wtf pandas xlsxwriter openpyxl - 修复代码缩进错误:代码中
if form.validate_on_submit():和return render_template(...)存在缩进错误,这会直接导致运行时语法异常,修正后再重新部署。 - 验证文件读写权限:确认Azure环境中
static/files目录已存在且应用有读写权限,部署时需确保该目录及abc.xlsx文件已上传至服务器。 - 添加异常捕获逻辑:在文件读取、数据处理环节加入try-except块,捕获异常并返回明确提示,避免泛泛的500错误:
try: df1 = pd.read_excel(prop_file) df_Check = pd.read_excel(file) # 后续数据处理逻辑 except Exception as e: return f"处理出错: {str(e)}", 400 - 关闭Debug模式:生产环境部署时不要启用
debug=True,改为app.run()或使用Azure指定的启动命令,debug模式存在安全风险且可能引发环境兼容问题。
内容的提问来源于stack exchange,提问作者r228302
相关产品推荐
相关产品推荐

