无法在Flask中指定端口。

huangapple go评论54阅读模式
英文:

No way to specify a port in Flask

问题

在Flask v2.2的文档中:

要么识别并停止其他程序,要么使用flask run --port 5001选择不同的端口。

然后:

$ flask --app my_web.py --port 8021 run
用法:flask [选项] 命令 [参数]...
尝试 'flask --help' 以获取帮助。

错误:没有这样的选项:--port

是怎么回事?

英文:

In the documenation of Flask v2.2

Either identify and stop the other program, or use flask run --port 5001 to pick a different port.

Then

$ flask --app my_web.py --port 8021 run
Usage: flask [OPTIONS] COMMAND [ARGS]...
Try 'flask --help' for help.

Error: No such option: --port

What's the matter?

答案1

得分: 0

你可以通过以下方式为DEV更改端口:

flask --app app.py run --port 8021

如果你要部署到生产环境,请使用Gunicorn(直接使用Flask部署到生产环境不推荐,因为它不稳定且不是为生产环境设计的)。在生产环境中使用不同端口的示例如下:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

然后在终端中运行以下命令:

gunicorn --bind 127.0.0.1:8021 app:app
英文:

You change the port for DEV by doing the following

flask --app app.py run --port 8021

If you're deploying to production, use Gunicorn (using flask directly to deploy to production is not recommended because it is not stable and wasn't designed for prod). This is what using a different port in prod would look like:

from flask import Flask

app = Flask(__name__)

@app.route(&quot;/&quot;)
def hello_world():
    return &quot;&lt;p&gt;Hello, World!&lt;/p&gt;&quot;

Then I would run this in the terminal

gunicorn --bind 127.0.0.1:8021  app:app

huangapple
  • 本文由 发表于 2023年4月20日 01:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定