python - 在 OpenShift 中以 HTTPS 模式配置 python flask 应用程序
问题描述
我正在尝试在 openshift 4.7 中以 https 模式运行 python 烧瓶应用程序。我的 python 烧瓶应用程序在端口 8080 上侦听,该端口通过 dockerFile 配置公开,如下所示。我已经通过 Openshift 控制台中的配置重定向了 httpsService Port Mapping
请求https port 443 to pod port 8080
。您可以通过转到 OpenshiftConsole > Project > Services > Service Details
并选择您的应用程序来进行配置。但是我仍然无法以 https 模式访问该服务。当我尝试以 https 模式访问时,出现错误The application is currently not serving requests at this endpoint. It may not have been started or is still starting.
当我通过 oc cli 进行正常部署并且不通过 openshift 控制台进行任何 ssl 配置时,该应用程序在 http 模式下运行良好。请告知如何在 https 模式下运行它
我的 app.py 代码如下
from flask import Flask, jsonify, request, redirect
app = Flask(__name__)
@app.route('/<string:name>/')
def helloName(name):
print(request)
return "Hello " + name
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug = True)
我的 Dockerfile 内容如下,我正在从中暴露端口 8080
# set base image
FROM registry.redhat.io/rhel8/python-38
# copy the dependencies file to the working directory
COPY requirements.txt .
# Configure PIP with local Nexus
ENV PIP_TRUSTED-HOST=nexus.company.com:8443
ENV PIP_INDEX=https://nexus.company.com:8443/repository/pypi-group/pypi
ENV PIP_INDEX-URL=https://nexus.company.com:8443/repository/pypi-group/simple
ENV PIP_NO-CACHE-DIR=false
ENV PIP_TIMEOUT=600
RUN touch ~/.netrc && \
echo "machine nexus.company.com" >> ~/.netrc && \
echo "login ${NEXUS_USER}" >> ~/.netrc && \
echo "password ${NEXUS_TOKEN}" >> ~/.netrc
# install dependencies
RUN pip install -U pip \
pip install -r requirements.txt
# copy the content of the local src directory to the working directory
COPY src/ .
EXPOSE 8080
# command to run on container start
CMD [ "python3", "./app.py" ]
Requirements.txt 只有 Flask
解决方案
这里有两件事:
使用 host="0.0.0.0" Flask 侦听它正在运行的机器的 IP。我认为将其更改为 127.0.0.1 应该很好。
app.run(host='127.0.0.1', port=8080, debug = True)
可能更重要的
.run()
是,Flask 中的方法仅用于开发,对于生产,您应该使用 gunicorn 之类的东西。
干杯,T
推荐阅读
- django - Django 抽象模型继承 - 使所有字段都可以为空
- php-7 - 特征函数的 PHPDoc 返回类型
- c# - XAML 数据绑定到另一个类
- java - Springboot JMS消费者代码在tomcat中部署为WAR时不运行
- python - 在 MatplotLib 中绘制日期和小时
- python - Flake8:如何将选项传递给旧版 api?
- c# - 将 localhost 端口设置为环境变量
- excel - SSIS 包在环境之间的行为不同
- spring-security - @ManagementContextConfiguration 与 WebSecurityConfigurerAdapter 一起使用不起作用
- xml - 如何在控制台上显示 XML http-response 或其任何值