Python 如何解决这些flask 500服务器错误?

Python 如何解决这些flask 500服务器错误?,python,apache,amazon-web-services,flask,webserver,Python,Apache,Amazon Web Services,Flask,Webserver,我现在被难倒了。我最近在生产ec2服务器上部署了一个大型flask应用程序,但该应用程序运行不正常。它在dev/test中运行良好。看起来,也许,阿帕奇被绞死了。当我第一次部署它时,它工作了几次。然后,在那之后,我开始收到500个服务器错误。当我重新启动apache时,会话再次正常工作,但500个服务器错误又回来了。每次,在应用程序的不同页面上。这种情况再次发生。我该如何着手解决这个问题?以下是我的路线/app.py: from flask import Flask, render_templa

我现在被难倒了。我最近在生产ec2服务器上部署了一个大型flask应用程序,但该应用程序运行不正常。它在dev/test中运行良好。看起来,也许,阿帕奇被绞死了。当我第一次部署它时,它工作了几次。然后,在那之后,我开始收到500个服务器错误。当我重新启动apache时,会话再次正常工作,但500个服务器错误又回来了。每次,在应用程序的不同页面上。这种情况再次发生。我该如何着手解决这个问题?以下是我的路线/app.py:

from flask import Flask, render_template,redirect,request,url_for, flash, session
from Index_generator import index_generator
from Yelp_api import request_yelp
from plaid import auth
from search_results import search,search1
from Options import return_data
from datetime import timedelta
import os
import urllib2
import time
from path import path_data
#from writer import create_config_file
import logging
from logging_path import log_dir



logger = logging.getLogger("Routing")
logger.setLevel(logging.INFO)
foodie_log = log_dir()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %  (message)s')
foodie_log.setFormatter(formatter)
logger.addHandler(foodie_log)

app = Flask(__name__)

app.secret_key = os.urandom(24)
go = path_data()


@app.before_request
def make_session_permanent():
    session.permanent = False
    app.permanent_session_lifetime = timedelta(seconds=300)
#@app.route('/setup',methods=['GET','POST']) 
#def setup():
#   if request.method == 'POST':
#        Username=request.form['username']
#        Password=request.form['password']
#        Port=request.form['port']
#        Host=request.form['host']
#        C_Key=request.form['CONSUMER_KEY']
#        C_Sec=request.form['CONSUMER_SECRET']
#        Tok=request.form['TOKEN']
#        Tok_Sec=request.form['TOKEN_SECRET']
#        clientid=request.form['client_id']
#        Sec=request.form['secret']
#        create_config_file(Username,Password,Port,Host,C_Key,C_Sec,Tok,Tok_Sec,clientid,Sec)
#        return 'complete'
#    elif request.method == 'GET':
#
#        return render_template('setup.html')

@app.route('/')
def home():
    store = index_generator()
    session['store'] = store
    return render_template('home.html')

@app.route('/about')
def about():
  return render_template('about.html')

@app.route('/home_city',methods = ['POST'])
def home_city():
    try:
        CITY=request.form['city']
        store = session.get('store')
        request_yelp(DEFAULT_LOCATION=CITY,data_store=store)
        return render_template('bank.html')
    except Exception as e:
        logger.error(e)
        error = 'Sorry, no results. Is' + ' ' +CITY + ' '+ 'your hometown? If not, try again and if so, we have been made aware of the issue and is working to resolve it'
        return render_template('home.html',error=error)
@app.route('/traveling',methods = ['POST'])
def travel():
    store = session.get('store')
    answer=request.form['Travel']
    if answer == 'yes':
        #time.sleep(2)
        return render_template('destination.html')
    else:
        results_home = search(index=store)
        time.sleep(2)
        return return_data(results_home)


@app.route('/dest_city',methods = ['POST'])
def dest_city():
    store = session.get('store')
    try:
        DESTINATION=request.form['dest_city']
        request_yelp(DEFAULT_LOCATION=DESTINATION,  data_store=store,sourcetype='dest_city')
        results_dest = search1(index=store)
        time.sleep(2)
        return return_data(results_dest)
     except urllib2.HTTPError:
        error = 'Sorry, no results. Is your destination city? If not, try again and if so, we have been made aware of the issue and is working to resolve it'
       return render_template('destination.html',error=error)


@app.route('/bank',methods = ['POST'])
def bank():
   try:
        store = session.get('store')
        print store
        Bank=request.form['Fin_Bank']
        Username=request.form['username']
        Password=request.form['password']
        Test =  auth(account=Bank,username=Username,password=Password,data_store=store)
        if Test == 402 or Test ==401:
            error = 'Invalid credentials'
            return render_template('bank.html',error=error)
        else :
            return render_template('travel.html')

    except:
        logger.error(e)
if __name__ == '__main__':
  app.run(debug=True)
  app.secret_key=os.urandom(24)
不完全确定这是否会导致您的错误,但您肯定应该更改:

您必须将
app.secret\u key
设置为静态值,而不是
os.uradom(24)
,例如:

app.secret_key = "/\xfa-\x84\xfeW\xc3\xda\x11%/\x0c\xa0\xbaY\xa3\x89\x93$\xf5\x92\x9eW}"

使用当前代码,应用程序的每个工作线程将具有不同的密钥,这可能会导致错误或会话不一致,具体取决于您在应用程序中使用会话对象的方式。

谢谢,我会尝试。我正在使用会话获取该用户在页面输入时创建的数据存储,以便应用程序中其他进程生成的数据可以为该用户使用相同的数据存储。我不知道原始海报,但我遇到了类似的问题,这就是解决方案。和往常一样,回想起来完全有道理,但当应用程序每三次重新加载时就抛出一个错误时,它完全令人困惑。