To run https functionality or SSL authentication in flask application you first install "pyOpenSSL" python package using:
pip install pyopenssl
Next step is to create 'cert.pem' and 'key.pem' using following command on terminal :
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
Copy generated 'cert.pem' and 'kem.pem' in you flask application project
Add ssl_context=('cert.pem', 'key.pem') in app.run()
For example:
from flask import Flask, jsonify
app = Flask(__name__)
@app.route('/')
def index():
return 'Flask is running!'
@app.route('/data')
def names():
data = {"names": ["John", "Jacob", "Julie", "Jennifer"]}
return jsonify(data)
if __name__ == '__main__':
app.run(ssl_context=('cert.pem', 'key.pem'))