Home python How to provide HTTPS in flask python?

How to provide HTTPS in flask python?

Author

Date

Category

How to provide https connection on flask web framework (python3)? Thanks in advance!


Answer 1, authority 100%

Like this:

from flask import Flask
from OpenSSL import SSL
context = SSL.Context (SSL.PROTOCOL_TLSv1_2)
context.use_privatekey_file ('server.key')
context.use_certificate_file ('server.crt')
app = Flask (__ name__)
@ app.route ('/')
def index ():
  return 'Hello, world!'
if __name__ == '__main__':
   app.run (host = '127.0.0.1', debug = True, ssl_context = context)

But it’s not a good practice to handle HTTPS inside a web application. For this, proxy servers are usually used like NginX , examples of its configuration are complete, including on the official website .

Where to get an SSL / TLS certificate is a separate question with its own nuances – there are many articles (especially in English), google it. In short: you can buy, you can generate a simple one yourself or on sites like SSLforFree .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions