开发者社区> 问答> 正文

flask想监听在HTTPS上面,但是运行报错

部分代码

if __name__ == '__main__':
    # app.run(host="0.0.0.0",port=5004)
    app.run(host="0.0.0.0", port=443, ssl_context=(
        "/Users/jiangyd/Downloads/dc/ca/serevr.crt",
        "/Users/jiangyd/Downloads/dc/ca/server.key"))

报错如下

Traceback (most recent call last):
  File "app.py", line 76, in <module>
    "/Users/jiangyd/Downloads/dc/ca/serevr.crt",
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/flask/app.py", line 922, in run
    run_simple(t.cast(str, host), port, self, **options)
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 1008, in run_simple
    inner()
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 957, in inner
    fd=fd,
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 781, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 702, in __init__
    ssl_context = load_ssl_context(*ssl_context)
  File "/Users/jiangyd/PycharmProjects/untitled16/venv/lib/python3.7/site-packages/werkzeug/serving.py", line 595, in load_ssl_context
    ctx.load_cert_chain(cert_file, pkey_file)
FileNotFoundError: [Errno 2] No such file or directory

展开
收起
道月芬1 2021-10-21 11:17:14 1632 0
2 条回答
写回答
取消 提交回答
  • 监听https需要配置正确的证书,要确定能使用的证书格式和位置

    2021-10-22 10:38:18
    赞同 展开评论 打赏
  • 网络规划设计师、敏捷专家、CISP、ITSS服务经理、ACA全科目、ACP4项、ACE、CBP、CDSP、CZTP等。拥有 PRINCE2 Foundation/Practitioner、CCSK、ITIL、ISO27001、PMP等多项国际认证。 专利5+、期刊10+、知识产权师。核心期刊审稿人。

    您好,从图中意思可看出应该是SSL证书路径不对导致。您pyopenssl 模块安装的话,如果没有证书请生成证书:

    pip install pyopenssl
    
    
    openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
    
    2021-10-21 11:22:55
    赞同 1 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
CDN助力企业网站进入HTTPS时代 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载