Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 integrate Digest Authentication into the web server but I cannot seem to find out how to get https using FLASK if you can show me how please comment on what i would need to do with the code below to make that 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() 
like image 713
robm Avatar asked Apr 05 '15 14:04

robm


People also ask

How do you add HTTPS in Flask?

In a Flask application, there are two ways through which we can build the Flask application and launch it through HTTPS. One of the methods is using ssl_context in the main() section of the code, and the other is through the command-line interface using the –cert option.

Can Flask handle HTTP requests?

Flask has different decorators to handle http requests. Http protocol is the basis for data communication in the World Wide Web. Used to send HTML form data to the server. The data received by the POST method is not cached by the server.


2 Answers

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) 
like image 188
Anand Tripathi Avatar answered Sep 21 '22 05:09

Anand Tripathi


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) 
like image 22
RobM Avatar answered Sep 23 '22 05:09

RobM