Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Https with Http in Flask Python

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?

like image 994
Yasmin Reda Avatar asked Sep 23 '13 14:09

Yasmin Reda


People also ask

Does Flask use http or https?

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.

How do I make a https request in Flask?

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.

Can Flask handle HTTP requests?

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.


2 Answers

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.

like image 130
Tritium21 Avatar answered Sep 21 '22 18:09

Tritium21


My I suggest trying out Flask-SSLify - https://github.com/kennethreitz/flask-sslify

Usage

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

Install

Installation is simple too:

$ pip install Flask-SSLify
like image 20
John Drefahl Avatar answered Sep 19 '22 18:09

John Drefahl