Serving Flask app with waitress on windows
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.
Related videos on Youtube
llulai
Updated on April 12, 2022Comments
-
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 almost 6 yearshave you tried setting host to 0.0.0.0?
-
llulai almost 6 yearsyes, same result
-
matth almost 6 yearsDoes 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 almost 6 yearsyes 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 theWireless LAN adapter Wi-Fi
-
matth almost 6 yearsWhich 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 almost 6 yearsI'm not using the one given by whatismyip4.com (anyway, neither of those work :/ )
-
llulai almost 6 yearswhen I use the local address in the local machine, it works, when I use it in another device, it doesn't
-
llulai almost 6 yearsI just realized that can be reached by computers in the same network, but not from computers outside of the network
-
-
Harsha Biyani almost 6 yearsplease add some explanation
-
Soren about 5 yearsAnd then you have two files lying around. But what to do with it?
-
Dondon Jie about 5 yearsDid you mean how to run? ``` python waitress_server.py ```
-
EpixyCoder101 over 3 yearsI use
waitress-serve --port=5000 main:app
-
EpixyCoder101 over 3 yearsYou 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 over 2 yearshi, 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