How to deploy flask backend with waitress server to the internet?

3.8k Views Asked by At

I programmed a flask backend, and made it work on my local network (wifi, ethernet etc). However I can't manage to expand it so external searches reach it. The code for the backend looks like this:

import os
from flask import Flask, flash, request, redirect, url_for, send_from_directory
from waitress import serve
other imports...

app = Flask(__name__)
app.secret_key = os.urandom(24)
.....
if __name__ == '__main__':
serve(app,host='0.0.0.0',port=5000)

How should I give the server an external IP?

1

There are 1 best solutions below

2
On

If I can make a suggestion, did you try using gevent? It provides a WSGI standalone server for you to replace the built-in option shipped with Flask.

It is very straightforward to use it:

pip install gevent

And you can plug into your app like this:

import os
from gevent.pywsgi import WSGIServer # Imports the WSGIServer
from gevent import monkey; monkey.patch_all() 
from flask import Flask, flash, request, redirect, url_for, send_from_directory


app = Flask(__name__)
app.secret_key = os.urandom(24)


if __name__ == '__main__':
    LISTEN = ('0.0.0.0',5000)

    http_server = WSGIServer( LISTEN, app )
    http_server.serve_forever()

Gevent also provides support for SSL

You can use it on its own or along with gunicorn or circusd I hope it helps you!