Serving Flask app with waitress on windows
Asked Answered
G

9

30

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?

Goldschmidt answered 26/6, 2018 at 14:56 Comment(8)
have you tried setting host to 0.0.0.0?Heterotrophic
yes, same resultGoldschmidt
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?Sutra
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-FiGoldschmidt
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?Sutra
I'm not using the one given by whatismyip4.com (anyway, neither of those work :/ )Goldschmidt
when I use the local address in the local machine, it works, when I use it in another device, it doesn'tGoldschmidt
I just realized that can be reached by computers in the same network, but not from computers outside of the networkGoldschmidt
D
66

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 
Drilling answered 30/8, 2018 at 9:36 Comment(4)
And then you have two files lying around. But what to do with it?Engrossment
Did you mean how to run? ``` python waitress_server.py ```Drilling
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? thanksWindblown
How does this work if someone typed in digital.local and expecting to see the website? How does it work in that particular scenario?Dorathydorca
C
13

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.

Chemosmosis answered 28/4, 2019 at 20:52 Comment(2)
I use waitress-serve --port=5000 main:appFredericfrederica
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__).Fredericfrederica
L
6

Try using

serve(app, host='0.0.0.0', port=8080)
Lentic answered 28/6, 2018 at 7:51 Comment(1)
please add some explanationPycno
E
4
from flask import Flask
from waitress import serve

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello stay healthy.</p>"

if __name__ == "__main__":
    serve(app, host="127.0.0.1", port=8080)
  1. Problem may persist in host. You can use host="127.0.0.1" in your program.
  2. Save your program in app.py file.
  3. Run your program.
  4. The server will be accessible at http://localhost:8080
Extenuate answered 30/1, 2023 at 4:9 Comment(1)
just to add something for people who are new to python. with the above change, just run python app.py (if your file is saved as app.py)Myrta
R
2

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")
Replenish answered 28/12, 2021 at 17:45 Comment(0)
T
1

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.

Treatment answered 26/1, 2020 at 23:20 Comment(0)
I
0

I just realized that can be reached by computers in the same network, but not from computers outside of the network

You need to forward the port in your router and use your public IP address.

Intercommunion answered 6/9, 2018 at 7:37 Comment(0)
L
0

To be able to use your internal PC (behind the router) you need to forward in the router the externl port 8080 to internal port 8080 and IP address of your server.

In this conditions you can access your server from outside your network using your external IP. That is OK if you have a static IP address allocated by your provider. If not than you can use a free DNS provider (I use DnsExit) which will provide you with a name for your external IP address. This way you can access your server with a name even if the IP address from your service provider changes from time to time.

Libelous answered 31/3, 2020 at 0:24 Comment(0)
D
-1

Set the host IP to 0.0.0.0 which means listen on all addresses.

Next, add a line in the /etc/hosts file pointing to your public IP address and assign it to your domain e.g. <your's public IP address www.exapmle.com>.

After you have opened the hosts file, also add your local IP address and the hostname of server machine e.g. <192.168.1.21 ServerHostname>.

This will save you time in the future.

Dearden answered 7/4 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.