Can you add HTTPS functionality to python flips web server?

I am trying to create a web interface to create a boot interface on a network device that uses a network device using Digest Authentication and HTTPS. I figured out how to integrate digest authentication into a web server, but I can't figure out how to get https using FLASK if you can show me how to comment on what I need to do with the code below for this to happen.

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() 
+22
python rest flask
source share
5 answers

this also works as a last resort

 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) 
+17
source

The code

 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) 

Do not use openssl or pyopenssl, now it becomes deprecated in python

+13
source

Deploy Flask on a real web server, not on an embedded server (for development).

See the Deployment Options chapter of the Flask documentation. Servers such as Nginx and Apache can control the configuration of HTTPS servers, not HTTP servers for your site.

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

+10
source
  • To run the https function or SSL authentication in a jar application, you first install the python package "pyOpenSSL" using:

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

      openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365 
  • Copy the generated cert.pem and kem.pem files to the bulb application project

  • Add ssl_context = ('cert.pem', 'key.pem') to 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')) 
+5
source

If this web server is for testing and demonstration purposes only. You can use ngrok, an open source that tunnels your http traffic.

Bascially ngrok creates a public URL (both http and https), and then tunnels the traffic to any port running the Flask process.

https://ngrok.com/product

It only takes a couple of minutes to set up. First you need to download the software. Then run the command
. / ngrok http [port number on which your python process is running]

Then it will open a window in the terminal, providing you with both http and https-url to access your web application.

+3
source

All Articles