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

后端 未结 6 2078
半阙折子戏
半阙折子戏 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 03:59

    Deploy Flask on a real web server, rather than with the built-in (development) server.

    See the Deployment Options chapter of the Flask documentation. Servers like Nginx and Apache both can handle setting up HTTPS servers rather than HTTP servers for your site.

    The standalone WSGI servers listed would typically be deployed behind Nginx and Apache in a proxy-forwarding configuration, where the front-end server handles the SSL encryption for you still.

    0 讨论(0)
  • 2020-11-28 04:03

    If this webserver is only for testing and demoing purposes. You can use ngrok, a open source too that tunnels your http traffic.

    Bascially ngrok creates a public URL (both http and https) and then tunnels the traffic to whatever port your Flask process is running on.

    https://ngrok.com/product

    It only takes a couple minutes to set up. You first have to download the software. Then run the command
    ./ngrok http [port number your python process is running on]

    It will then open up a window in terminal giving you both an http and https url to access your web app.

    0 讨论(0)
  • 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'))
    
    0 讨论(0)
  • 2020-11-28 04:18

    this also works in a pinch

    from flask import Flask, jsonify
    
    
    from OpenSSL import SSL
    context = SSL.Context(SSL.PROTOCOL_TLSv1_2)
    context.use_privatekey_file('server.key')
    context.use_certificate_file('server.crt')
    
    
    
    
    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()
    if __name__ == '__main__':  
         app.run(host='127.0.0.1', debug=True, ssl_context=context)
    
    0 讨论(0)
  • 2020-11-28 04:19

    Don't use openssl or pyopenssl its now become obselete in python

    Refer the Code below

    from flask import Flask, jsonify
    import os
    
    ASSETS_DIR = os.path.dirname(os.path.abspath(__file__))
    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__':
        context = ('local.crt', 'local.key')#certificate and key files
        app.run(debug=True, ssl_context=context)
    
    0 讨论(0)
  • 2020-11-28 04:24

    For a quick n' dirty self-signed cert, you can also use flask run --cert adhoc or set the FLASK_RUN_CERT env var.

    $ export FLASK_APP="app.py"
    $ export FLASK_ENV=development
    $ export FLASK_RUN_CERT=adhoc
    
    $ flask run
     * Serving Flask app "app.py" (lazy loading)
     * Environment: development
     * Debug mode: on
     * Running on https://127.0.0.1:5000/ (Press CTRL+C to quit)
     * Restarting with stat
     * Debugger is active!
     * Debugger PIN: 329-665-000
    

    The adhoc option isn't well documented (for good reason, never do this in production), but it's mentioned in the cli.py source code.

    There's a thorough explanation of this by Miguel Grinberg at Running Your Flask Application Over HTTPS.

    0 讨论(0)
提交回复
热议问题