Heroku 上的 Flask 项目 returns 500 内部服务器错误
Flask projects on heroku returns 500 internal server error
我有一个基本的 flask 项目并将其部署到 heroku。
和页面 returns 500 内部服务器错误。我没有使用数据库。
这是我的日志:
2015-01-10T23:40:03.161880+00:00 heroku[web.1]: Starting process with command `gunicorn print:app --log-file=-`
2015-01-10T23:40:03.827603+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Using worker: sync
2015-01-10T23:40:03.827516+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Listening at: http://0.0.0.0:5144 (2)
2015-01-10T23:40:03.835308+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [8] [INFO] Booting worker with pid: 8
2015-01-10T23:40:03.887218+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [9] [INFO] Booting worker with pid: 9
2015-01-10T23:40:03.826883+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Starting gunicorn 19.1.1
2015-01-10T23:40:04.268774+00:00 heroku[web.1]: State changed from starting to up
2015-01-10T23:41:19.847544+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=a2450ab9-3183-473f-aa0e-8e970b266b28 fwd="88.238.99.0" dyno=web.1 connect=1ms service=26ms status=500 bytes=456
2015-01-10T23:41:20.206409+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=topstreaks.herokuapp.com request_id=1c969a6b-a8e7-4fcd-b84a-81b55cec18a6 fwd="88.238.99.0" dyno=web.1 connect=1ms service=2ms status=404 bytes=527
2015-01-10T23:41:24.949004+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=7f90b786-3ec9-49ab-b51a-a2cd942bc83d fwd="88.238.99.0" dyno=web.1 connect=1ms service=13ms status=500 bytes=456
2015-01-10T23:44:05.320256+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=8c0a51bd-459c-438e-8184-911557410c95 fwd="88.238.99.0" dyno=web.1 connect=3ms service=3ms status=500 bytes=456
2015-01-10T23:44:05.697401+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=topstreaks.herokuapp.com request_id=59f43b94-fb30-414a-9a28-6ae7c195bd4b fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms status=404 bytes=527
2015-01-10T23:48:29.595955+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=13941ed3-2496-4d0e-aacc-5fb6a7a122e9 fwd="88.238.99.0" dyno=web.1 connect=3ms service=2ms status=500 bytes=456
2015-01-10T23:48:47.739233+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=6c370ced-99fe-429a-829b-0ea55b4aa508 fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms status=500 bytes=456
2015-01-10T23:52:15.563473+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=a0ceaf25-359e-4bc1-9247-5d6f478d1dd2 fwd="88.238.99.0" dyno=web.1 connect=0ms service=1ms status=500 bytes=456
2015-01-10T23:52:20.050874+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=1086dffb-e144-4873-b49b-5e4ac44477f3 fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms sta
tus=500 bytes=456
服务器查找,但我无法在日志或任何地方找到内部错误。它在本地运行良好。
我怎么能在 heroku 上找到错误,因为尽管我在 flask 上写了 debug = True
,但 debug mod 不起作用。
烧瓶代码:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('topstreaks.html')
@app.route('/script')
def script():
return render_template('datum.js')
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == "__main__":
app.debug = True
app.run()
如何调试错误?
我在本地尝试过这个,发现你可以通过在你的应用程序中设置一个记录器并将其打印到标准输出来查看问题所在
from flask import Flask, render_template
import sys
import logging
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
这样当你在 heroku 上 运行 时,任何错误都会打印在 heroku logs
上
有趣的是,在尝试设置它进行测试时,我 运行 遇到了与您的问题类似的问题。模板 (html) 必须位于项目根目录的模板文件夹中,我的测试模板不是,这对我造成了 500,这也可能是你的 500 的原因。
我有一个基本的 flask 项目并将其部署到 heroku。 和页面 returns 500 内部服务器错误。我没有使用数据库。 这是我的日志:
2015-01-10T23:40:03.161880+00:00 heroku[web.1]: Starting process with command `gunicorn print:app --log-file=-`
2015-01-10T23:40:03.827603+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Using worker: sync
2015-01-10T23:40:03.827516+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Listening at: http://0.0.0.0:5144 (2)
2015-01-10T23:40:03.835308+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [8] [INFO] Booting worker with pid: 8
2015-01-10T23:40:03.887218+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [9] [INFO] Booting worker with pid: 9
2015-01-10T23:40:03.826883+00:00 app[web.1]: [2015-01-10 23:40:03 +0000] [2] [INFO] Starting gunicorn 19.1.1
2015-01-10T23:40:04.268774+00:00 heroku[web.1]: State changed from starting to up
2015-01-10T23:41:19.847544+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=a2450ab9-3183-473f-aa0e-8e970b266b28 fwd="88.238.99.0" dyno=web.1 connect=1ms service=26ms status=500 bytes=456
2015-01-10T23:41:20.206409+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=topstreaks.herokuapp.com request_id=1c969a6b-a8e7-4fcd-b84a-81b55cec18a6 fwd="88.238.99.0" dyno=web.1 connect=1ms service=2ms status=404 bytes=527
2015-01-10T23:41:24.949004+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=7f90b786-3ec9-49ab-b51a-a2cd942bc83d fwd="88.238.99.0" dyno=web.1 connect=1ms service=13ms status=500 bytes=456
2015-01-10T23:44:05.320256+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=8c0a51bd-459c-438e-8184-911557410c95 fwd="88.238.99.0" dyno=web.1 connect=3ms service=3ms status=500 bytes=456
2015-01-10T23:44:05.697401+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=topstreaks.herokuapp.com request_id=59f43b94-fb30-414a-9a28-6ae7c195bd4b fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms status=404 bytes=527
2015-01-10T23:48:29.595955+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=13941ed3-2496-4d0e-aacc-5fb6a7a122e9 fwd="88.238.99.0" dyno=web.1 connect=3ms service=2ms status=500 bytes=456
2015-01-10T23:48:47.739233+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=6c370ced-99fe-429a-829b-0ea55b4aa508 fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms status=500 bytes=456
2015-01-10T23:52:15.563473+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=a0ceaf25-359e-4bc1-9247-5d6f478d1dd2 fwd="88.238.99.0" dyno=web.1 connect=0ms service=1ms status=500 bytes=456
2015-01-10T23:52:20.050874+00:00 heroku[router]: at=info method=GET path="/" host=topstreaks.herokuapp.com request_id=1086dffb-e144-4873-b49b-5e4ac44477f3 fwd="88.238.99.0" dyno=web.1 connect=0ms service=2ms sta
tus=500 bytes=456
服务器查找,但我无法在日志或任何地方找到内部错误。它在本地运行良好。
我怎么能在 heroku 上找到错误,因为尽管我在 flask 上写了 debug = True
,但 debug mod 不起作用。
烧瓶代码:
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('topstreaks.html')
@app.route('/script')
def script():
return render_template('datum.js')
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
return response
if __name__ == "__main__":
app.debug = True
app.run()
如何调试错误?
我在本地尝试过这个,发现你可以通过在你的应用程序中设置一个记录器并将其打印到标准输出来查看问题所在
from flask import Flask, render_template
import sys
import logging
app = Flask(__name__)
app.logger.addHandler(logging.StreamHandler(sys.stdout))
app.logger.setLevel(logging.ERROR)
这样当你在 heroku 上 运行 时,任何错误都会打印在 heroku logs
有趣的是,在尝试设置它进行测试时,我 运行 遇到了与您的问题类似的问题。模板 (html) 必须位于项目根目录的模板文件夹中,我的测试模板不是,这对我造成了 500,这也可能是你的 500 的原因。