搭建一个flask的helloworld并且使用自签证书用https访问他
发布日期:2021-05-10 06:21:05 浏览次数:21 分类:精选文章

本文共 2713 字,大约阅读时间需要 9 分钟。

������������Flask���Hello World������������������������������HTTPS������

������������������������������Flask���������������������������������������HTTPS���������������������������������������������������������������������������������������

Step 1: ������Flask������

������������������������������������Flask���

pip install flask

Step 2: ������Flask������

������������Python������������������������������Flask���������

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'

Step 3: ������Flask���������

������������������Flask������HTTP���������

if __name__ == '__main__':
app.run(debug=True, port=5000)

������������HTTPS������������������������������ssl_context���������

if __name__ == '__main__':
app.run(debug=True, port=5000, ssl_context='adhoc')

Step 4: ���������������������

���������������������������������������������������Ubuntu 20.04 LXD���������������������

  • ������������������
  • mkdir -p ca
    cd ca
    1. ���������������
    2. openssl genrsa -des3 -out server.key 1024
      1. ���������������������������CSR������
      2. openssl req -new -key server.key -out server.csr
        1. ���������������
        2. openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
          1. ������������������
          2. cp server.key server.key.org

            ���������ca ��������������������� server.crt ��� server.key��������������������������������� server.key������������������

            Step 5: ������Flask���������������������������

            ���������Flask������������������������������

            from flask import Flask
            app = Flask(__name__)
            @app.route('/')
            def hello_world():
            return 'Hello, World!'
            # ������������������������app���������
            if __name__ == '__main__':
            app.run(debug=True, port=5000, ssl_context=(r'/path/to/server.crt', r'/path/to/server.key'))

            ���������r'/path/to/server.crt' ��� r'/path/to/server.key' ������������������������������������������

            Step 6: ������Flask������������������

            ���������������

            python app.py

            ������������������������ https://localhost:5000���

            ���������������������������������������������������������������������������������������������������

            sh -c "echo -e '\n\n*!' | tee /etc/ssl/certs/*.crt.pem && loca��octl --enable-crt /path/to/server.crt"

            ��� server.crt ������������������������������������������������������������������������������������������������������������������

            Step 7: ������������������������������������

            ��������������������������������������������� CA ���895794UNDLE���������������������������������������������������������������������������������������������������������������

            ������

            ���������������������������������������������Flask���������������������������HTTPS���������������������������������������������������������������������������������������������������������������

    上一篇:nginx配置https
    下一篇:SpringBoot发送邮件

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月13日 02时50分44秒