Using HTTPS on the Django/flask development server

Source: Internet
Author: User
Tags openssl library ssl certificate stunnel
When developing Web apps using Django or Flask, it is common to develop and debug programs with built-in servers, which are then transferred to the production environment for deployment. The problem is that these built-in servers usually do not support HTTPS, we want to be able to use and test https at development time, do not want to deploy to the production environment without testing, so we need the 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 to 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 over the network;
Decrypts the encrypted data stream and sends the decrypted traffic to another program over the network.
After understanding the function of Stunnel we can easily think of using Stunnel to establish an SSL encrypted channel bound to the Django/flask built-in 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 sends data after processing to Stunnel and then encrypts it back to the browser user.

Well, it says a bunch of seemingly complex, actually using Stunnel is very simple.

Install Stunnel on the server where the Django/flask development server is located:

# yum Install Stunnel (on CentOS)

Or

$ sudo apt-get install stunnel4 (on Ubuntu)

If you do not have to purchase an SSL certificate, you generate one, by the way, 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 configuration file called HTTPS, and then use Stunnel to execute this configuration file and start port 443 to connect to port 8000 of the Django/flask built-in server:

# vi Httpspid =cert = Vpsee.pemdebug = 7foreground = yes[https]accept = 443connect = 8000# Stunnel HTTPS

Start the Django built-in server bound to port 8000 mentioned above in the configuration file:

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

# vi run.py#!flask/bin/pythonfrom app import appapp.run (host= ' 0.0.0.0 ', port=8000, debug = True) #./run.py * Running on HT Tp://0.0.0.0:8000/* Restarting with Reloader
  • Related Article

    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.