I have a client server application. I managed to make them connect over https using SSl encryption using this
context = SSL.Context(SSL.SSLv3_METHOD)
context.use_privatekey_file('/path_to_key/key.key')
context.use_certificate_file('/path_to_cert/cert.crt')
app.run(use_reloader=True, host='0.0.0.0',port=9020,ssl_context = context)
Now I want to run the server using both http and https. Is there any possible way to do that?
Flask HTTPS is defined as a concept that allows developers to develop Flask applications and deploy them for production use through HTTPS, which are complaint to encrypted connections thus providing extra security.
You can use ngrok, a open source too that tunnels your http traffic. Bascially ngrok creates a public URL (both http and https) and then tunnels the traffic to whatever port your Flask process is running on. It will then open up a window in terminal giving you both an http and https url to access your web app.
Flask has different decorators to handle http requests. Http protocol is the basis for data communication in the World Wide Web. Used to send HTML form data to the server. The data received by the POST method is not cached by the server.
First big thing: don't use the built in web server in flask to do any heavy lifting. You should use a real web server like apache (mod_wsgi) nginex + gunicore, etc. These servers have documentation on how to run http and https simultaneously.
My I suggest trying out Flask-SSLify - https://github.com/kennethreitz/flask-sslify
Usage is pretty simple:
from flask import Flask
from flask_sslify import SSLify
app = Flask(__name__)
sslify = SSLify(app)
If you make an HTTP request, it will automatically redirect:
$ curl -I http://secure-samurai.herokuapp.com/
HTTP/1.1 302 FOUND
Content-length: 281
Content-Type: text/html; charset=utf-8
Date: Sun, 29 Apr 2012 21:39:36 GMT
Location: https://secure-samurai.herokuapp.com/
Server: gunicorn/0.14.2
Strict-Transport-Security: max-age=31536000
Connection: keep-alive
Installation is simple too:
$ pip install Flask-SSLify
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