我使用以下方法生成了私钥和自签名证书:
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)
OR
if __name__ == '__main__':
app.run(host='127.0.0.1', ssl_context=context, debug=True)
最后,
python app.py
但是,它不能在https上运行。 如何以https://localhost:5000身份运行?
答案 0 :(得分:0)
生成证书和ssl_context
设置的方式有效。
我猜你用Flask命令运行应用程序flask run
,但是您的ssl_context
设置位于if __name__ == '__main__':
语句中,这意味着只有直接执行此文件(例如, python app.py
,假设文件名为app.py
。
默认情况下,flask在http上运行应用程序,因此缺少https。