如何解决这些烧瓶500服务器错误?

时间:2015-07-24 12:34:09

标签: python apache amazon-web-services flask webserver

我现在非常难过。我有一个大瓶应用程序,我最近部署到我的生产ec2服务器,该服务器运行不正常。它在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)

1 个答案:

答案 0 :(得分:2)

完全确定这是否会导致您的错误,但您一定要改变这一点:

您必须将app.secret_key设置为静态值而不是os.urandom(24),例如:

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

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