部署在Heroku上的Flask应用程序出现H10错误

4

我的应用在本地主机上运行得非常完美,但是我将flask应用程序部署到heroku时遇到了以下错误:

2020-07-01T09:56:02.982007+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=webscrappingapp.herokuapp.com request_id=59cec0a7-98a3-49d8-b980-779cada2c0d9 fwd="103.40.197.230" dyno= connect= service= status=503 bytes= protocol=https
2020-07-01T09:56:03.765421+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=webscrappingapp.herokuapp.com request_id=b2ddd2b8-5930-4049-993d-3c8e972b10ee fwd="103.40.197.230" dyno= connect= service= status=503 bytes= protocol=https

Flask file, webscrap.py--

from flask import Flask, render_template

import requests
from bs4 import BeautifulSoup

from datetime import datetime
from pytz import timezone

#__name__ == __main__
app = Flask(__name__)
URL = 'https://techcrunch.com/'
page = requests.get(URL)  # type of page -- request

# type coverted to BeautifulSoup
# second argument how you want to structure your data
soup = BeautifulSoup(page.content, 'html.parser')

results = soup.find(class_='river river--homepage')

post_elems = results.select('.post-block')

table = [['Sr No', 'Time Stamp', 'News Title', 'Image Source URL', 'Author']]
i = 1

for post_elem in post_elems:
    header_elem = post_elem.find('header', class_='post-block__header')
    title_elem = header_elem.find('a', class_='post-block__title__link')
    timeStamp_elem = header_elem.find('time')['datetime']
    author_elem = header_elem.find('span')
    image_elem = post_elem.find('img')
    date = datetime.strptime(timeStamp_elem, "%Y-%m-%dT%H:%M:%S%z" )
    row = [i,  date.astimezone(timezone('Asia/Kolkata')).strftime("%I:%M %p" + " IST " + "%B %d, %Y"), title_elem.text.strip(),
           image_elem.get('src'), author_elem.text.strip()]
    table.append(row)
    i = i + 1

@app.route('/')
def index():
    return render_template("index.html", table=table)

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

Procfile:

web : gunicorn webscrap:app

Requirements.txt:

autopep8==1.5.3
beautifulsoup4==4.9.1
certifi==2020.6.20
chardet==3.0.4
click==7.1.1
Flask==1.1.2
gunicorn==20.0.4
idna==2.10
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
pycodestyle==2.6.0
pytz==2020.1
requests==2.24.0
soupsieve==2.0.1
toml==0.10.1
urllib3==1.25.9
Werkzeug==1.0.1

我尝试用以下内容替换Procfile文件的内容:

web : gunicorn --bind 0.0.0.0:$PORT webscrap:app

但它对我没有起作用。我还尝试将app替换为server,但仍然没有起作用。所以,请针对这个问题提出解决方案。

编辑: 我的错误在于Procfile中的空格。正确的是--

web: gunicorn webscrap:app

但现在我遇到了H14错误,尝试在Procfile中添加heroku ps:scale web=1,但对我没有用。


我不确定这个代码是否在gunicorn下被执行,但在Heroku上你不能以debug模式运行Flask:app.run(debug=True)。应该是False或省略。你也可以在那里指定一个端口app.run(port=os.environ.get("PORT", 8080))(将绑定到PORT如果存在,否则使用端口8080)。还可以尝试在没有gunicorn的情况下运行Flask应用程序,希望得到更有意义的错误消息。 - Tin Nguyen
3个回答

4

将Procfile txt更改为

web: uwsgi uwsgi.ini

创建新文件uwsgi.ini并添加以下代码

[uwsgi]
http-socket = :$(PORT)
master = true
die-on-term = true
module = app:app
memory-report = true

在 requirement.txt 中添加 uwsgi。

...
urllib3==1.25.9
Werkzeug==1.0.1
uwsgi

添加 runtime.txt,并添加以下文本

python-version

例如 python-3.7.4。

我需要同时包含.flaskenv文件吗? - Gourav Aggarwal
不需要添加.env文件。您如何将应用程序部署到Heroku? - Usman Ali
你的项目在GitHub上吗?如果在GitHub上,那么如果你愿意,我可以自己检查它。 - Usman Ali
你也可以查看我在Heroku上部署的项目:https://github.com/2016CS107/fyp-project/tree/master/Backend%20API - Usman Ali
我忘了添加 runtime.txt 文件,但它仍然可以工作(终于!)。现在还在想那个文件是用来做什么的? - nurp
显示剩余4条评论

0

我的项目结构

My Project Structure 创建 Procfile - 在其中编写以下行 web:gunicorn main: app 其中“main”是文件名,其中提到了app=create_app()

pip install gunicorn

pip freeze > requirements.txt

创建另一个名为runtime.txt的文件,其中包含以下行 python-3.9.2 (提供您项目的相应python版本)

Heroku 部分:使用 gitbash 或 vscode 控制台逐个运行以下命令

heroku login

git init

git add .

git commit -am "Initial Commit"

git push heroku main


0
首先:单词“Procfile”应该以大写字母开头,然后将Procfile保存在与应用程序文件夹相同的目录中。
其次,请尝试在Procfile中使用以下代码: web: gunicorn app:app --preload 并不要忘记保存所做的更改。
最后,转到终端并应用git push heroku master 希望这可以帮助您。

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接