Run HTTPS on Flask Web Server

Flask usually has an HTTP protocol while launching the web server. Notification libraries and a few others require to meet HTTPS protocol. Some tweaks and tricks can help us to switch to HTTPS.

HTTP: Standard protocol to transfer data packets over the internet without any encryption. Tools like Wireshark can easily capture your packets.

HTTPS: Secure version of the HTTP protocol. It encrypts all data packets into cipher, which can only be decrypted using a valid private key.

How to Create HTTPS Server in Node Js

Now you will learn how to switch flask web server from HTTP to HTTPS protocol.

Install Python, Flask, and OpenSSL

One or two applications are required to be installed depending upon your system—Python and Flask for starting web server and OpenSSL to generate self-signed certificates.

Linux: These packages are available to install within the system repository. Execute the below command in your terminal to install all necessary packages.

$ sudo apt update
$ sudo apt install python
$ sudo apt install python-pip

OpenSSL is available by default for every Linux distribution.

Windows 10/11: These packages are available to install in standalone packages. To download python, click here. For OpenSSL, you can use the git bash console.

Generating Certificates

You have to generate a self-signed certificate along with its key. They are used to check the site’s authorities and encrypt the data packets.

For Linux users, open your terminal using Ctrl+Alt+T and for Windows users can open your git bash console from the start menu and type the below command.

$ openssl req -x509 --newkey rsa:4096 --out cert.pem --keyout key.pem --days 365

Next, it will ask general questions like country name in two-letter code, email, organization name, etc. It’s up to you which information you want to provide they are not required.

Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

Two files will be generated with the name “cert.pem” and “key.pem”. Please create a new folder with name cert in your project directory and move them there.

Writing Code

Below is the basic template we traditionally use to launch a standard HTTP web server.

from flask import *

app = Flask(__name__)

@app.route("/")
def home():
    return "TREND OCEANS"

if __name__=="__main__":
    app.run(port=4000)

To start an HTTPS server using the above code requires implementing a self-signed certificate. After three essential things are needed to create a secure server.

  1. Generating secret key.
  2. Assigning certificate and key to a variable.
  3. Specifying that variable to run function.

All three points are implemented in the below code.

from flask import *

app = Flask(__name__)
app.secret_key = "abc" #Secret Key

@app.route("/")
def home():
    return "TREND OCEANS"

if __name__=="__main__":
    context = ('./cert/cert.pem', './cert/key.pem') #Location of certificate & key
    app.run(port=4000, ssl_context=context) #Specify variable to run function

Save the file and launch your Flask web server.

Leave a Reply