How to host a proper Python Flask server with HTTP, HTTPS and interactive debug shell, all in the same global namespace?

37 views Asked by At

In Python Flask, if you run app.run(), typically you get the following message:

WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.

I know how to properly host a Flask server using WSGI and gunicorn. However, how to do that in a more complicated usage case? For example, in the following code,

@app.route("/f_home")
def f_home():
 return doA() if request.url_root.startswith('https') else doB()

threading.Thread(target=lambda:app.run(host='0.0.0.0', port=8000, threaded = True)).start()
threading.Thread(target=lambda:app.run(host='0.0.0.0', port=8001, threaded = True, ssl_context=('cert.pem', 'key.pem')).start()

import IPython
IPython.embed()

Firstly, HTTPS and HTTP requests need to be handled differently, e.g. in f_home(). Moreover, the same process hosts both the HTTP and HTTPS in separate threads, but having the same global name scope (because some clients connect via HTTP, some via HTTPS, but they all need to be managed together), and at the same time launch an interactive debugging console that can inspect and debug everything at run time.

0

There are 0 answers