繁体   English   中英

如何在 flask 中使用 HTTPS 运行 localhost?

[英]How to run localhost with HTTPS in flask?

我使用以下方法生成了私钥和自签名证书:

openssl req -x509 -out localhost.crt -keyout localhost.key \
  -newkey rsa:2048 -nodes -sha256 \
  -subj '/CN=localhost' -extensions EXT -config <( \
   printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")

创建的context如下:

from OpenSSL import SSL

context = SSL.Context(SSL.TLSv1_2_METHOD)
context.use_certificate('localhost.crt')
context.use_privatekey('localhost.key')

并以两种方式运行 flask 应用程序(均无效):

if __name__ == '__main__':
    app.run(ssl_context=('localhost.crt', 'localhost.key'), debug=True)

或者

if __name__ == '__main__':
    app.run(host='127.0.0.1', ssl_context=context, debug=True)

最后,

python app.py

然而,它不能在 https 上运行。 我该如何运行: https://localhost:5000

您生成证书和ssl_context设置的方式是有效的。

I'm guessing you run the application with Flask command eg flask run but your ssl_context setup is inside if __name__ == '__main__': statement meaning it will be executed this way only if you execute this file directly eg python app.py assuming that该文件名为app.py

默认情况下 flask 在 http 上运行应用程序,因此它缺少 https。

暂无
暂无

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

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