Serving Flask app with waitress on windows

2020-06-03 00:55发布

问题:

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?

回答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 


回答2:

Try using

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


回答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 simply use:-

waitress-serve --call "myapplication:create_app"

This command will launch the server listening on port 8080 by default.

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"


NB: click on the image if it's not very clear.

Waitress serve command line arguments.

Flask 1.0 production deployment tutorial.



回答4:

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.



回答5:

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.



回答6:

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.