MAC OS platform uses Python and Docker to create tests with Https Server

Source: Internet
Author: User
Tags openssl x509 csr certificate docker run

This is an article created on, where the information may have evolved or changed.
Mac OS platform uses Python and Docker to create Https Server for testing
Flask is a Python Web Framework that I like very much. Recently I need to test Https communication. I need to create an Https Server that uses a self-signed certificate. Then Flask can be easily achieved through the following steps.

Self-signed certificate
Use OpenSSL to generate the .key private key file and the .csr Certificate Signing Request file:

openssl req -new -key test.key -out test.csr
Generate a 10-year self-signed certificate file:

openssl x509 -req -days 3650 -in test.csr -signkey test.key -out test.crt
For more information, please refer to the HTTPS certificate chain and HTTPS implementation issues in Android applications.

Flask project
I will not mention the development environment of Python. The command line is very simple. Installing Flask is a one-line command:

pip install flask
Then, create a Python source file with the following content:

from OpenSSL import SSL
from flask import Flask
from flask import render_template

app = Flask (__ name__)
context = SSL.Context (SSL.SSLv23_METHOD)
context.use_privatekey_file ('/ Users / rwang / testssl / test.key')
context.use_certificate_file ('/ Users / rwang / testssl / test.crt')


@ app.route ("/")
def hello ():
return render_template ("index.html")

if __name__ == "__main__":
    app.run (host = '127.0.0.1', port = 8443,
        debug = True, ssl_context = context)
You can see that the self-signed certificate just generated is configured on the Https test site, and the Flask template is used to respond to the contents of an index.html file. This needs to create the templates directory under the same level directory as the Python source files above, and create the index.html file, which is related to the test purpose, such as jumping to the Http site:

<html>
  <head>
    <title> Redirect </ title>
    <script> window.location.href = 'http: //www.baidu.com'; </ script>
  </ head>
  <body>
      <h1> Hello, Stranger! </ h1>
  </ body>
</ html>
From here, you can execute the following Python source code on the command line, and visit https://127.0.0.1:8443 in your browser to check whether the Https site is working properly:

python pyhttpsserver.py
If you jump to the Baidu homepage normally, it means the site is working properly.

Docker
Now, in addition to Boot2Docker, Docker officially provides the file for installing Docker on the Mac OS platform. After downloading, follow the prompts to install it.

After installation, refer to the official document above to create a VirtualBox and start it to perform daily management of Docker. Here, create a Dockerfile file in the folder where the above pyhttpsserver.py file and templates directory are stored, the content is:

FROM python: 2.7.10
RUN pip install Flask
ADD. / Code
WORKDIR / code
CMD python pyhttpsserver.py
To build a Docker image using the Dockerfile above, you need to pull down python: 2.7.10 base image:

docker pull python: 2.7.10
Then cd in the shell to the directory containing the Dockerfile and run the following command to build an image containing this Https Server:

docker build -t httpsserver.
Pay attention to the last. No less.

With httpsserver image, you can run this Https Server Container in the background:

docker run -d httpsserver
Well, we have a ready-to-use Https test server. If we want to use it for other purposes, modify the Flask project and recompile different Docker images. When the container is mounted in the container and then executed, you can refer to Understanding Volumes in Docker for details.

-EOF-

HTTPS certificate chain and HTTPS implementation problems in Android applications → ← BLE and BLE on Android
Disclaimer: This article uses the BY-NC-SA protocol for authorization. Reprint please indicate from: Mac OS platform uses Python and Docker to create test Https Server

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.