简体   繁体   English

部署在 heroku 上时,Flask 应用程序出现 H10 错误

[英]H10 Error in Flask App when deployed on heroku

My app is working perfectly on local host but I am getting the following error when I deployed my flask app on heroku我的应用程序在本地主机上运行良好,但是当我在 heroku 上部署 flask 应用程序时出现以下错误

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-- Flask 文件,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:要求.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

I tried replacing the Procfile content with我尝试将 Procfile 内容替换为

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

But it didn't worked for me.但这对我没有用。 I also tried replacing app with server but didn't worked.我也尝试用server替换app ,但没有奏效。 So, please suggest the solution for this problem.所以,请提出这个问题的解决方案。

EDIT: My mistake was spacing in Procfile.编辑:我的错误是 Procfile 中的间距。 Correct one is --正确的是——

web: gunicorn webscrap:app

But now I am getting H14 error also tried adding heroku ps:scale web=1 in Procfile but didn't worked for me.但是现在我收到 H14 错误,也尝试在 Procfile 中添加heroku ps:scale web=1但对我没有用。

Change Procfile txt to将 Procfile txt 更改为

web: uwsgi uwsgi.ini

create new file uwsgi.ini and add this code创建新文件 uwsgi.ini 并添加此代码

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

Add uwsgi in requirement.txt在requirement.txt中添加uwsgi

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

Add runtime.txt and add this text添加 runtime.txt 并添加此文本

python-version

for example python-3.7.4例如 python-3.7.4

My Project Structure我的项目结构

我的项目结构 Create Procfile - write a following line into it web: gunicorn main:app where "main" is the file name in which app = create_app() is mentioned创建 Procfile - 在其中写入以下行web: gunicorn main:app其中“main”是提到app = create_app()的文件名

pip install gunicorn

pip freeze > requirements.txt

create one more file named runtime.txt which contains following line python-3.9.2 (provide respective python version of your project)再创建一个名为runtime.txt的文件,其中包含以下行python-3.9.2 (提供项目的相应 python 版本)

Heroku Part: run following commands one by one using gitbash or vscode console Heroku 部分:使用 gitbash 或 vscode 控制台一一运行以下命令

heroku login

git init

git add.

git commit -am "Initial Commit"

git push heroku main

first of all: the word "Procfile" should be started with capital letter then keep the procfile in the same directory of the app folder.首先:“Procfile”一词应以大写字母开头,然后将 procfile 保存在 app 文件夹的同一目录中。

secondly, try the following code in Procfile web: gunicorn app:app --preload and don't forget to save changes made in the Procfile其次,在 Procfile web: gunicorn app:app --preload并且不要忘记保存在 Procfile 中所做的更改

finally, go to the terminal and apply git push heroku master最后,go 到终端并应用git push heroku master

hope this help you希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM