使用 Heroku 部署 Flask [英] Deploying Flask with Heroku
问题描述
我正在尝试将 Flask 应用程序部署到 Heroku,但是在推送代码时出现错误
I'm trying to deploy a Flask app to Heroku however upon pushing the code I get the error
2013-06-23T11:23:59.264600+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
我不知道该尝试什么,我尝试将端口从 5000 更改为 33507,但无济于事.我的 Procfile 看起来像这样:
I'm not sure what to try, I've tried changing the port from 5000 to 33507, but to no avail. My Procfile looks like this:
web: python main.py
main.py
是启动服务器的主要 Flask 文件.
main.py
is the main Flask file which initiates the server.
谢谢.
推荐答案
在我托管在 Heroku 上的 Flask 应用程序中,我使用此代码启动服务器:
In my Flask app hosted on Heroku, I use this code to start the server:
if __name__ == '__main__':
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
在本地开发时,这将使用端口 5000,在生产中 Heroku 将设置 PORT
环境变量.
When developing locally, this will use port 5000, in production Heroku will set the PORT
environment variable.
(旁注:默认情况下,Flask 只能从您自己的计算机访问,不能从网络中的任何其他计算机访问(请参阅 Quickstart.设置 host='0.0.0.0'
将使 Flask 从网络可用)
(Side note: By default, Flask is only accessible from your own computer, not from any other in the network (see the Quickstart). Setting host='0.0.0.0'
will make Flask available from the network)
这篇关于使用 Heroku 部署 Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!