0

I am trying to run a flask app with waitress serve, but when i call "python app.py" nothing happens (i waited 2 minutes then ctrl+c). When i run flask with app.run() everything works normally. Some help would be appreciated

import Flask
from waitress import serve

app=Flask(__name__)
<My code>
if __name__ == "__main__":
  serve(app,port="8080",host="0.0.0.0")
Matias
  • 3
  • 3
  • Does this answer your question? [Serving Flask app with waitress on windows](https://stackoverflow.com/questions/51045911/serving-flask-app-with-waitress-on-windows) – noah1400 Jul 21 '22 at 10:46
  • There are multiple WSGI solutions, see e.g. https://quintagroup.com/cms/python/web-server Try other services. If they don't work your flask app may not be compatible with them. You are running multiple instances of the flask app and if they access the same file, use the same login or service etc. it can fail. Furthermore you should be showing the full traceback error or any logs. – Tin Nguyen Jul 21 '22 at 10:46
  • @noah1400 I have already tried that and the powershell does nothing until I CTRL+C. On the other hand, when i write ```waitress-serve --host 127.0.0.1 "app:app" ``` everything works. – Matias Jul 21 '22 at 10:56
  • The shell shows nothing but the server still starts. If you run it on `127.0.0.1` – noah1400 Jul 21 '22 at 11:37

1 Answers1

3

Create a method called create_app() for example which returns the Flask object. You can add a route if you wish.

from flask import Flask

def create_app():
    app = Flask(__name__)

    @app.route('/')
    def hello_world():
      return 'Hello World!'
    return app

if your file is called app.py run the following command.

waitress-serve --port 8080 --call "app:create_app"

Output:

INFO:waitress:Serving on http://0.0.0.0:8080

Edit


If you really want to do it with waitress.serve() you can do this. For some reason this only works on localhost (127.0.0.1)

With your code the server starts and is accessable (the only thing you need are some routes) the only thing that is missing is terminal output.

You can activate logging like this:

import logging
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)

This causes the output:

INFO:waitress:Serving on http://127.0.0.1:8080

If you want to see every request: install paste pip install paste and use TransLogger

from paste.translogger import TransLogger
# modify serve function
serve(TransLogger(app, setup_console_handler=False), host='127.0.0.1', port=8080)

This causes the following output on every request.

INFO:wsgi:127.0.0.1 - - [21/Jul/2022:15:40:25 +0200] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"

Of course this depends on your browser.

Full code:

from flask import Flask
from waitress import serve
from paste.translogger import TransLogger
import logging
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)

app = Flask(__name__)

@app.route('/')
def hello():
  return 'Hello World!'


serve(TransLogger(app, setup_console_handler=False), host='127.0.0.1', port=8080)
noah1400
  • 1,282
  • 1
  • 4
  • 15