Serving Flask app with waitress on windows

58,603

Solution 1

Simple example,try it!
I hope it will help you.

app1.py

from flask import Flask
app = Flask(__name__)
# app.run(host='0.0.0.0', port=8080,debug=True)

waitress_server.py

from waitress import serve
import app1
serve(app1.app, host='0.0.0.0', port=8080)

Then run below command

python waitress_server.py 

Solution 2

Try using

serve(app, host='0.0.0.0', port=8080)

Solution 3

Waitress now provides a simple command line Utility called waitress-serve for running the Flask Application. Please note that this answer is valid for Waitress 1.30. The command line arguments could change in future.

If your Flask application is called myapplication and the method which instantiates your application is called create_app, then you can run the command below. This will launch the server listening on port 8080 by default.

  • waitress-serve --call "myapplication:create_app"

If you wish to launch it on port 80 (http), then all you need to do is:

  • waitress-serve --port=80 --call "myapplication:create_app"
D:\flaskapps>waitress-serve --port 80 --call "dlrlsummarizer:create_app"

Serving on http://ADITHYA-PC:80

Waitress serve command line arguments.

Flask 1.0 production deployment tutorial.

Solution 4

I ran into this question and looking something similar. After looking at the documentation and combined with your original request, I tested

serve(app, port=8080, host="x.x.x.x")

Where x.x.x.x is my host ip address. It works fine on my end.

Complete code

from flask import Flask
from waitress import serve

app = Flask(__name__)
...
serve(app, port=8080, host="x.x.x.x")

Solution 5

I realize this question was probably based in a miss-diagnosed firewall or NAT issue, but in case people come here actually wanting to serve a Flask app with waitress on windows properly (as a service), I want to point to my answer here, so that it can be of use and receive some feedback.

Share:
58,603

Related videos on Youtube

llulai
Author by

llulai

Updated on April 12, 2022

Comments

  • llulai
    llulai about 2 years

    I am able to run a webserver using the following code

    from flask import Flask
    from waitress import serve
    
    app = Flask(__name__, static_url_path='/static')
    ...
    serve(app, port=8080)
    

    The problem is that I can access it only from the machine where it is running, if I try to access it using the ipv4 ip, it doesn't work. Am I missing a step?

    • johnII
      johnII almost 6 years
      have you tried setting host to 0.0.0.0?
    • llulai
      llulai almost 6 years
      yes, same result
    • matth
      matth almost 6 years
      Does your computer sit behind a home router (or other NAT routing device?) If so, which "ipv4 ip" are you using? And, is the other computer on the same home network?
    • llulai
      llulai almost 6 years
      yes it is behind a NAT routing device, and it is connected to the same network. I'm using the one that says IPv4 Address under the Wireless LAN adapter Wi-Fi
    • matth
      matth almost 6 years
      Which IP address are you using? The local address (that might be like 192.168.1.2, or like 10.x.x.x) or the global address (the one you might get from whatismyip4.com )? If you want to use the global address, you'll need to edit your router's configuration. Also, have you considered whether your Windows firewall is blocking access?
    • llulai
      llulai almost 6 years
      I'm not using the one given by whatismyip4.com (anyway, neither of those work :/ )
    • llulai
      llulai almost 6 years
      when I use the local address in the local machine, it works, when I use it in another device, it doesn't
    • llulai
      llulai almost 6 years
      I just realized that can be reached by computers in the same network, but not from computers outside of the network
  • Harsha Biyani
    Harsha Biyani almost 6 years
    please add some explanation
  • Soren
    Soren about 5 years
    And then you have two files lying around. But what to do with it?
  • Dondon Jie
    Dondon Jie about 5 years
    Did you mean how to run? ``` python waitress_server.py ```
  • EpixyCoder101
    EpixyCoder101 over 3 years
    I use waitress-serve --port=5000 main:app
  • EpixyCoder101
    EpixyCoder101 over 3 years
    You can format it like this: waitress-serve --port=port number program file:app variable Where app variable is the app variable For example, app = Flask(__name__).
  • Jun711
    Jun711 over 2 years
    hi, I know that waitress serve defaults to 4 threads. docs.pylonsproject.org/projects/waitress/en/stable/api.html If the machine that I use cannot handle 4 threads, do I have to set it lower? Do you know how to configure that? thanks