python - 需要帮助修复 500 错误 python/flask 应用程序

标签 python heroku twilio gunicorn

我正在尝试使用 Twilio API 在 flask 框架上运行一个简单的 python 应用程序。我正在使用以下依赖项在 virtualenv 中进行开发:

  • flask
  • Jinja2
  • 标记安全
  • 工作组
  • 参数分析
  • unicorn
  • httplib2
  • 它很危险
  • 六个
  • twilio
  • 单元测试2
  • wsgiref

当我使用工头运行时,我的索引页面呈现良好并加载 TwiML 正常,但我正在处理短信响应的/sms 页面继续收到 500 错误,“服务器遇到内部错误,无法完成您的请求. 要么是服务器过载,要么是应用程序有错误。”如果我推送到 Heroku,也会发生同样的错误。下面是我的代码,有什么想法吗?

from flask import Flask, request, redirect, session
import twilio.twiml
from twilio.rest import TwilioRestClient
import os

SECRET_KEY = 'a secret key'

#initialize app
app = Flask(__name__)
app.config.from_object(__name__)

@app.route('/')
def index():
    return 'hello'

@app.route("/sms", methods = ['GET', 'POST'])
def sms():  
    """Respond with the number of text messages sent between two parties."""
    counter = session.get('counter', 0)
    counter += 1
    if counter > 6:
        counter = 1
    session['counter'] = counter

    score = session.get('score', 0)
    answer = request.values.get('Body', '')
    answer_response = False
    response = False
    if counter == 1:
        response = "Welcome to our healthcare knowledge quiz. First question: What’s the average annual premium for family coverage on an employer health plan?"
    elif counter == 2:
        answer_response = "No, the average annual premium for family coverage on an employer health plan is $15,745."
        if answer.lower() == 'b':
            answer_response = "That's correct!"
            score += 1
        response = "Second question: What percentage of employer health premiums do workers pay, on average? A) 27.4%, B) 17.1%, C) 50.3%, or D) 5.8%"
    elif counter == 3:
        answer_response = "No, the average percentage of employer health premiums paid by workers is 27.4%"
        if answer.lower() == 'a':
            answer_response = "That's correct!"
            score += 1
        response = "3rd question: When do Americans have to purchase health insurance or face a penalty? A) January 2015, B) January 2014, C) December 2013, or D) December 2015."
    elif counter == 4:
        answer_response = "No, most Americans have to purchase health insurance or face a penalty under the federal healthcare law by January 2014."
        if answer.lower() == 'b':
            answer_response = "That's correct!"
            score += 1
        response = "4th question: What percentage of U.S. small businesses offered health benefits to their workers in 2010? A) 49%, B) 69%, C) 32%, or D) 58%."
    elif counter == 5:
        answer_response = "No, the percentage of U.S. small businesses which offered health benefits in 2010 was 49%."
        if answer.lower() == 'a':
            answer_response = "That's correct!"
            score += 1
        response = "5th question: How many people under 26 have been added to health plans as part of the Affordable Care Act? A) 5.6 mil., B) 0.5 mil., C) 2.9 mil., or D) 1.3 mil."
    elif counter == 6:
        answer_response = "No, 2.9 million young adults under age 26 have been added to parents' health plans as part of the Affordable Care Act."
        if answer.lower() == 'c':
            answer_response = "That's correct!"
            score +=1
        response = "Thanks for taking the healthcare quiz! You correctly answered %d out of 5 questions" % score

    session['score'] = score

    resp = twilio.twiml.Response()
    if answer_response:
        resp.sms(answer_response)
    resp.sms(response)
    return str(resp)

if __name__ == "__main__":
    app.debug = True
    app.run()

凯文伯克

Heroku 日志:

2013-09-25T05:50:03.933994+00:00 heroku[web.1]: Starting process with command `gunicorn app_SMS_demo:app`
201
3-09-25T05:50:04.812490+00:00 app[web.1]: 2013-09-25 05:50:04 [2] [INFO] Handling signal: term
2013-09-25T05:50:04.812967+00:00 app[web.1]: 2013-09-25 05:50:04 [7] [INFO] Worker exiting (pid: 7)
2013-09-25T05:50:04.888387+00:00 app[web.1]: 2013-09-25 05:50:04 [2] [INFO] Shutting down: Master
2013-09-25T05:50:06.437279+00:00 app[web.1]: 2013-09-25 05:50:06 [2] [INFO] Starting gunicorn 18.0
2013-09-25T05:50:06.438326+00:00 app[web.1]: 2013-09-25 05:50:06 [2] [INFO] Listening at: http://0.0.0.0:53890 (2)
2013-09-25T05:50:06.438549+00:00 app[web.1]: 2013-09-25 05:50:06 [2] [INFO] Using worker: sync
2013-09-25T05:50:06.467209+00:00 app[web.1]: 2013-09-25 05:50:06 [7] [INFO] Booting worker with pid: 7
2013-09-25T05:50:06.998082+00:00 heroku[web.1]: State changed from starting to up
2013-09-25T05:50:07.065070+00:00 heroku[web.1]: Process exited with status 0
2013-09-25T05:50:10.438774+00:00 heroku[router]: at=info method=GET path=/ host=quiet-tundra-1590.herokuapp.com fwd="76.126.150.1" dyno=web.1 connect=1ms service=2648ms status=200 bytes=5
2013-09-25T05:52:44.200768+00:00 heroku[router]: at=info method=GET path=/ host=quiet-tundra-1590.herokuapp.com fwd="76.126.150.1" dyno=web.1 connect=2ms service=5ms status=200 bytes=5
2013-09-25T05:52:53.941483+00:00 heroku[router]: at=info method=GET path=/sms host=quiet-tundra-1590.herokuapp.com fwd="76.126.150.1" dyno=web.1 connect=1ms service=177ms status=500 bytes=291

最佳答案

Twilio 开发倡导者 Joël Franusic 向我介绍了一个解决方案。事实证明这是一个unicode问题。我使用来自另一个网站的文本来形成测验。在文本中是一个隐藏的 unicode 来源的撇号而不是预期的 UTF-8,这阻止了 Green Unicorn 运行。

经验教训 - 不要复制网页文本,重新键入所有内容,以防止错误,或者在文本编辑器或 IDE 上使用明显的字体,以便您可以分辨两种字体类型之间的区别。

关于python - 需要帮助修复 500 错误 python/flask 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18996479/

相关文章:

ruby-on-rails - 使用 Heroku 将 Zip 上传到 S3,upzip 并允许其他人使用唯一链接访问提取的文件

javascript - 注销 channel 监听器

python - 根据变量类别的计数绘制数据集

Python matplotlib : Change axis labels/legend from bold to regular weight

ruby-on-rails - 是否可以在 Heroku Cedar 上运行 capybara-webkit(即 fork 的 webkit_server)?

javascript - 从不同子域访问沙盒 iframe 中的摄像头和麦克风

ios - 无法向 Twilio 可编程聊天中的 channel 添加属性?

python - 何时在 Python 中使用 "while"或 "for"

python - 从未经训练的数据集中为 doc2vec 中的句子构建向量

node.js - 无法通过express/heroku查看Angular应用程序?