在页面的 Heroku return HTML 上托管 Flask API
Hosting Flask API on Heroku return HTML of page
当我尝试在 Heroku 上托管 Flask API 时,我在部署日志中没有收到任何错误(它甚至说“构建成功”),但是当我尝试执行 POST 请求,我只是得到页面的 HTML 而不是 JSON 数据。
它在本地主机上工作正常,问题只发生在 Heroku 上。由于服务器位于文件夹中而不是存储库的根目录中,因此我必须创建一个仅包含服务器文件的分支(称为 Heroku)。
端口有问题吗?
我要访问的路由:
@app.route('/api/model', methods = ["POST"])
def model():
try:
data = request.get_json()
print(f"Received model: {data}")
# `model_func()` is a function that returns a dictionary
return jsonify(model_func(data))
except:
print("Model didn't work")
if __name__ == "__main__":
app.run(host='localhost', port=os.environ.get('PORT'))
我如何访问它(邮递员):
Heroku 日志:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.9.5
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.9.5
-----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting Flask==2.0.0
Downloading Flask-2.0.0-py3-none-any.whl (93 kB)
Collecting Flask-Cors==3.0.10
Downloading Flask_Cors-3.0.10-py2.py3-none-any.whl (14 kB)
Collecting requests==2.22.0
Downloading requests-2.22.0-py2.py3-none-any.whl (57 kB)
Collecting eventlet==0.31.0
Downloading eventlet-0.31.0-py2.py3-none-any.whl (224 kB)
Collecting asyncio==3.4.3
Downloading asyncio-3.4.3-py3-none-any.whl (101 kB)
Collecting python-dotenv==0.17.1
Downloading python_dotenv-0.17.1-py2.py3-none-any.whl (18 kB)
Collecting numpy==1.20.3
Downloading numpy-1.20.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.4 MB)
Collecting scipy==1.6.3
Downloading scipy-1.6.3-cp39-cp39-manylinux1_x86_64.whl (27.3 MB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
Downloading click-8.0.1-py3-none-any.whl (97 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting Six
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
Collecting idna<2.9,>=2.5
Downloading idna-2.8-py2.py3-none-any.whl (58 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
Collecting chardet<3.1.0,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting greenlet>=0.3
Downloading greenlet-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (162 kB)
Collecting dnspython<2.0.0,>=1.15.0
Downloading dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl (30 kB)
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask, Six, Flask-Cors, certifi, idna, urllib3, chardet, requests, greenlet, dnspython, eventlet, asyncio, python-dotenv, numpy, scipy
Successfully installed Flask-2.0.0 Flask-Cors-3.0.10 Jinja2-3.0.1 MarkupSafe-2.0.1 Six-1.16.0 Werkzeug-2.0.1 asyncio-3.4.3 certifi-2021.5.30 chardet-3.0.4 click-8.0.1 dnspython-1.16.0 eventlet-0.31.0 greenlet-1.1.0 idna-2.8 itsdangerous-2.0.1 numpy-1.20.3 python-dotenv-0.17.1 requests-2.22.0 scipy-1.6.3 urllib3-1.25.11
-----> Discovering process types
Procfile declares types -> (none)
-----> Compressing...
Done: 100.2M
-----> Launching...
Released v18
https://pid-tuner-condig.herokuapp.com/ deployed to Heroku
URL 的 Heroku API: https://pid-tuner-condig.herokuapp.com/
URL 用于 GitHub 存储库: https://github.com/pmorim/pid-tuner/tree/heroku
您收到页面的 HTML 是因为您网站的服务器配置似乎有问题。当您打开网站的 link 时,您可以看到 heroku 显示应用程序错误来指示这一点。你有没有制作procfile?
当我尝试在 Heroku 上托管 Flask API 时,我在部署日志中没有收到任何错误(它甚至说“构建成功”),但是当我尝试执行 POST 请求,我只是得到页面的 HTML 而不是 JSON 数据。
它在本地主机上工作正常,问题只发生在 Heroku 上。由于服务器位于文件夹中而不是存储库的根目录中,因此我必须创建一个仅包含服务器文件的分支(称为 Heroku)。
端口有问题吗?
我要访问的路由:
@app.route('/api/model', methods = ["POST"])
def model():
try:
data = request.get_json()
print(f"Received model: {data}")
# `model_func()` is a function that returns a dictionary
return jsonify(model_func(data))
except:
print("Model didn't work")
if __name__ == "__main__":
app.run(host='localhost', port=os.environ.get('PORT'))
我如何访问它(邮递员):
Heroku 日志:
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.9.5
To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.9.5
-----> Installing pip 20.2.4, setuptools 47.1.1 and wheel 0.36.2
-----> Installing SQLite3
-----> Installing requirements with pip
Collecting Flask==2.0.0
Downloading Flask-2.0.0-py3-none-any.whl (93 kB)
Collecting Flask-Cors==3.0.10
Downloading Flask_Cors-3.0.10-py2.py3-none-any.whl (14 kB)
Collecting requests==2.22.0
Downloading requests-2.22.0-py2.py3-none-any.whl (57 kB)
Collecting eventlet==0.31.0
Downloading eventlet-0.31.0-py2.py3-none-any.whl (224 kB)
Collecting asyncio==3.4.3
Downloading asyncio-3.4.3-py3-none-any.whl (101 kB)
Collecting python-dotenv==0.17.1
Downloading python_dotenv-0.17.1-py2.py3-none-any.whl (18 kB)
Collecting numpy==1.20.3
Downloading numpy-1.20.3-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.4 MB)
Collecting scipy==1.6.3
Downloading scipy-1.6.3-cp39-cp39-manylinux1_x86_64.whl (27.3 MB)
Collecting Werkzeug>=2.0
Downloading Werkzeug-2.0.1-py3-none-any.whl (288 kB)
Collecting itsdangerous>=2.0
Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
Collecting click>=7.1.2
Downloading click-8.0.1-py3-none-any.whl (97 kB)
Collecting Jinja2>=3.0
Downloading Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting Six
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting certifi>=2017.4.17
Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
Collecting idna<2.9,>=2.5
Downloading idna-2.8-py2.py3-none-any.whl (58 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
Downloading urllib3-1.25.11-py2.py3-none-any.whl (127 kB)
Collecting chardet<3.1.0,>=3.0.2
Downloading chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting greenlet>=0.3
Downloading greenlet-1.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (162 kB)
Collecting dnspython<2.0.0,>=1.15.0
Downloading dnspython-1.16.0-py2.py3-none-any.whl (188 kB)
Collecting MarkupSafe>=2.0
Downloading MarkupSafe-2.0.1-cp39-cp39-manylinux2010_x86_64.whl (30 kB)
Installing collected packages: Werkzeug, itsdangerous, click, MarkupSafe, Jinja2, Flask, Six, Flask-Cors, certifi, idna, urllib3, chardet, requests, greenlet, dnspython, eventlet, asyncio, python-dotenv, numpy, scipy
Successfully installed Flask-2.0.0 Flask-Cors-3.0.10 Jinja2-3.0.1 MarkupSafe-2.0.1 Six-1.16.0 Werkzeug-2.0.1 asyncio-3.4.3 certifi-2021.5.30 chardet-3.0.4 click-8.0.1 dnspython-1.16.0 eventlet-0.31.0 greenlet-1.1.0 idna-2.8 itsdangerous-2.0.1 numpy-1.20.3 python-dotenv-0.17.1 requests-2.22.0 scipy-1.6.3 urllib3-1.25.11
-----> Discovering process types
Procfile declares types -> (none)
-----> Compressing...
Done: 100.2M
-----> Launching...
Released v18
https://pid-tuner-condig.herokuapp.com/ deployed to Heroku
URL 的 Heroku API: https://pid-tuner-condig.herokuapp.com/
URL 用于 GitHub 存储库: https://github.com/pmorim/pid-tuner/tree/heroku
您收到页面的 HTML 是因为您网站的服务器配置似乎有问题。当您打开网站的 link 时,您可以看到 heroku 显示应用程序错误来指示这一点。你有没有制作procfile?