can you add HTTPS functionality to a python flask web server?

后端 未结 6 2119
半阙折子戏
半阙折子戏 2020-11-28 03:46

I am trying to build a web interface to Mock up a restful interface on networking device this networking device uses Digest Authentication and HTTPS. I figured out how to in

6条回答
  •  北海茫月
    2020-11-28 04:16

    • To run https functionality or SSL authentication in flask application you first install "pyOpenSSL" python package using:

       pip install pyopenssl
      
    • Next step is to create 'cert.pem' and 'key.pem' using following command on terminal :

       openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
      
    • Copy generated 'cert.pem' and 'kem.pem' in you flask application project

    • Add ssl_context=('cert.pem', 'key.pem') in app.run()

    For example:

        from flask import Flask, jsonify
    
        app = Flask(__name__)
    
        @app.route('/')
    
        def index():
    
            return 'Flask is running!'
    
    
        @app.route('/data')
    
        def names():
    
            data = {"names": ["John", "Jacob", "Julie", "Jennifer"]}
    
            return jsonify(data)
    
      if __name__ == '__main__':
    
            app.run(ssl_context=('cert.pem', 'key.pem'))
    

提交回复
热议问题