I am trying to serve a Flask application over HTTPS using the flask
command. I can do this using app.run
by passing the ssl_context
argument, but I can't figure out how to do this on the CLI.
flask run --host='0.0.0.0' --port=80
In a Flask application, there are two ways through which we can build the Flask application and launch it through HTTPS. One of the methods is using ssl_context in the main() section of the code, and the other is through the command-line interface using the –cert option.
To run the app outside of the VS Code debugger, use the following steps from a terminal: Set an environment variable for FLASK_APP . On Linux and macOS, use export set FLASK_APP=webapp ; on Windows use set FLASK_APP=webapp . Navigate into the hello_app folder, then launch the program using python -m flask run .
Starting development server: Starting a development server in the flask using the following command. Here <name> is the name of the file where the instance of the flask app has been created and the app. run() function exists. By convention, this file is mostly named app, thus the command will be shown below.
Today, there are two ways to expose your flask application to the internet. Deploy the web application in your office server which has a public IP address and domain name. Deploy the web application in the cloud such as AWS, MS Azure, GCP or web hosting companies like GoDaddy, SiteGround, A2Hosting etc.
This will be available when Flask 1.0 is released.
flask run --cert dev.crt --key dev.key
Until then, use app.run
if you really need this. Keep in mind that the dev server is not meant for production and should typically not be accessible publicly, so SSL shouldn't be as big a concern.
I would personally use something like ngrok to create secure tunnels to localhost. Like davidism stated the dev server isn't meant for production but if you want see how your app behaves and/or your app requires SSL (like FlaskAsk apps for example) ngrok is the probably the easiest way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With