Reputation: 571
I generated private key and self-signed certificate using:
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")
Created context
as below:
from OpenSSL import SSL
context = SSL.Context(SSL.TLSv1_2_METHOD)
context.use_certificate('localhost.crt')
context.use_privatekey('localhost.key')
And ran flask app in two ways (none of them worked):
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)
Finally,
python app.py
Yet, it doesn't run on https. How can I run as: https://localhost:5000 ?
Upvotes: 0
Views: 6928
Reputation: 1853
The way you generate the certificate and the ssl_context
setup is working.
I'm guessing you run the application with Flask command e.g. 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 e.g. python app.py
assuming that the file is called app.py
.
By default flask runs app on http hence it is missing https.
Upvotes: 1