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 .