Serving Flask app with waitress on windows

2020-06-03 00:32发布

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?

6条回答
祖国的老花朵
2楼-- · 2020-06-03 01:02

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"


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

Waitress serve command line arguments.

Flask 1.0 production deployment tutorial.

查看更多
时光不老,我们不散
3楼-- · 2020-06-03 01:03

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 
查看更多
祖国的老花朵
4楼-- · 2020-06-03 01:11

Try using

serve(app, host='0.0.0.0', port=8080)
查看更多
可以哭但决不认输i
5楼-- · 2020-06-03 01:17

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.

查看更多
狗以群分
6楼-- · 2020-06-03 01:21

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.

查看更多
霸刀☆藐视天下
7楼-- · 2020-06-03 01:24

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.

查看更多
登录 后发表回答