28

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?

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
llulai
  • 627
  • 1
  • 6
  • 9
  • have you tried setting host to 0.0.0.0? – johnII Jun 26 '18 at 14:58
  • yes, same result – llulai Jun 26 '18 at 14:58
  • 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? – Robᵩ Jun 26 '18 at 15:01
  • 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` – llulai Jun 26 '18 at 15:03
  • 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 http://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? – Robᵩ Jun 26 '18 at 15:05
  • I'm not using the one given by whatismyip4.com (anyway, neither of those work :/ ) – llulai Jun 26 '18 at 15:09
  • when I use the local address in the local machine, it works, when I use it in another device, it doesn't – llulai Jun 26 '18 at 15:12
  • I just realized that can be reached by computers in the same network, but not from computers outside of the network – llulai Jun 26 '18 at 18:53

8 Answers8

64

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 
Dondon Jie
  • 833
  • 8
  • 11
  • 1
    And then you have two files lying around. But what to do with it? – Soerendip Mar 19 '19 at 21:30
  • 3
    Did you mean how to run? ``` python waitress_server.py ``` – Dondon Jie Mar 20 '19 at 05:39
  • hi, I know that waitress serve defaults to 4 threads. https://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 – Jun Sep 29 '21 at 22:57
  • How does this work if someone typed in https://digital.local and expecting to see the website? How does it work in that particular scenario? – pee2pee Jul 07 '22 at 08:46
12

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.

Adithya Upadhya
  • 2,239
  • 20
  • 28
  • I use `waitress-serve --port=5000 main:app` – Dukk Feb 17 '21 at 22:15
  • 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__)`. – Dukk Feb 17 '21 at 22:16
7

Try using

serve(app, host='0.0.0.0', port=8080)
Bosco
  • 1,536
  • 2
  • 13
  • 25
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")
Clement05
  • 61
  • 9
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.

Alias_Knagg
  • 886
  • 1
  • 7
  • 21
1
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
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.

Dominik
  • 1
  • 1
  • 1
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.

Gogu CelMare
  • 699
  • 6
  • 15