Using Https_python on the Django/flask development server

Source: Internet
Author: User
Tags openssl openssl library ssl certificate stunnel

Using the Django or Flask framework to develop web apps typically uses built-in server development and debugging programs, which are then transferred to the production environment when the program is completed. The problem is that these built-in servers typically do not support HTTPS, and we want to be able to use and test https at development time, not to deploy to a production environment without testing, so we need a built-in server to support HTTPS.

This problem can be solved by an external program stunnel, the role of Stunnel is to encrypt the TCP session through the OpenSSL Library, establish a secure channel, protect the program without encryption or unencrypted. Its main functions are two:

Receive unencrypted data stream, SSL encryption, and then send the encrypted data stream through the network;
Decrypts the encrypted data stream and sends the decrypted data stream over the network to another program.
After understanding the features of Stunnel, we can easily think of using Stunnel to establish an SSL encrypted channel bound to the Django/flask server, Stunnel start 443 port to accept the user's HTTPS request, decrypted and sent to the built-in server 8000 Port processing, the built-in server after processing the data sent to Stunnel and then encrypted back to the browser user.

Well, it said a bunch of seemingly complex, in fact, the use of Stunnel is very simple.

Install Stunnel on the same server as the Django/flask Development server:

# yum Install Stunnel (on CentOS)

Or

$ sudo apt-get install stunnel4 (on Ubuntu)

If you have not purchased an SSL certificate, generate one yourself, right, the permissions of this file must be 600 OH:

# OpenSSL req-new-x509-days 365-nodes-out vpsee.pem-keyout Vpsee.pem

# chmod

Create a new profile called HTTPS, and then execute the configuration file with Stunnel, starting port 443 to connect to port 8000 on the Django/flask server:

# vi HTTPS
PID =
cert = Vpsee.pem
debug = 7
foreground = yes

[HTTPS]
accept = 443
connect = 8

# Stunnel HTTPS

Start the Django build server binding to the 8000 ports mentioned in the above configuration file:

# Https=1 python manage.py runserver 0.0.0.0:8000
Starting the Flask server does not need to be special, change the port to 8000, start it in the normal way:

# VI run.py
#!flask/bin/python from
app import app
app.run (host= ' 0.0.0.0 ', port=8000, debug = True)

#./run.py
 * Running on http://0.0.0.0:8000/
 * Restarting with Reloader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.