Python 在 Windows 上与女服务员一起提供 Flask 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51045911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serving Flask app with waitress on windows
提问by llulai
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?
问题是我只能从运行它的机器访问它,如果我尝试使用 ipv4 ip 访问它,它不起作用。我错过了一步吗?
回答by Dondon Jie
Simple example,try it!
I hope it will help you.
简单的例子,试试吧!
我希望它会帮助你。
app1.py
应用程序1.py
from flask import Flask
app = Flask(__name__)
# app.run(host='0.0.0.0', port=8080,debug=True)
waitress_server.py
女服务员_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
回答by user2218085
Try using
尝试使用
serve(app, host='0.0.0.0', port=8080)
回答by Adithya Upadhya
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.
Waitress
现在提供了一个简单的命令行实用程序,waitress-serve
用于运行 Flask 应用程序。请注意,此答案适用于Waitress 1.30。命令行参数将来可能会更改。
If your Flask application is called myapplicationand the method which instantiates your application is called create_app, then you can simply use:-
如果您的 Flask 应用程序称为myapplication并且实例化您的应用程序的方法称为create_app,那么您可以简单地使用:-
waitress-serve --call "myapplication:create_app"
waitress-serve --call "myapplication:create_app"
This command will launch the server listening on port 8080by default.
默认情况下,此命令将启动侦听端口8080的服务器。
If you wish to launch it on port 80 (http), then all you need to do is:
如果您希望在端口80 (http)上启动它,那么您需要做的就是:
waitress-serve --port=80 --call "myapplication:create_app"
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.
Flask 1.0 生产部署教程。
回答by Alias_Knagg
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.
我意识到这个问题可能是基于错误诊断的防火墙或 NAT 问题,但是如果人们来到这里实际上想要在 Windows 上正确地提供带有女服务员的 Flask 应用程序(作为服务),我想在这里指出我的答案,以便它可以有用并获得一些反馈。
回答by Gogu CelMare
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.
为了能够使用您的内部 PC(在路由器后面),您需要在路由器中将外部端口 8080 转发到内部端口 8080 和服务器的 IP 地址。
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.
在这种情况下,您可以使用外部 IP 从网络外部访问您的服务器。如果您的提供商分配了静态 IP 地址,那就没问题了。如果不是,您可以使用免费的 DNS 提供商(我使用 DnsExit),它将为您提供外部 IP 地址的名称。这样,即使服务提供商的 IP 地址不时更改,您也可以使用名称访问服务器。
回答by Dominik
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.
您需要转发路由器中的端口并使用您的公共 IP 地址。