gpt4 book ai didi

python - 将 Flask 开发服务器配置为在整个网络中可见

转载 作者:行者123 更新时间:2023-12-04 19:43:57 26 4
gpt4 key购买 nike

我不确定这是否是 Flask 特有的,但是当我在开发模式下运行应用程序 ( http://localhost:5000 ) 时,我无法从网络上的其他机器访问它 (使用 http://[dev-host-ip]:5000 )。例如,在开发模式下使用 Rails,它可以正常工作。我找不到任何有关 Flask 开发服务器配置的文档。知道应该配置什么来启用它吗?

最佳答案

虽然这是可能的,但您不应在生产中使用 Flask 开发服务器。 Flask 开发服务器的设计并不是特别安全、稳定或高效。请参阅 deploying 上的文档以获得正确的解决方案。
--host flask run 的选项,或 host app.run() 的参数, 控制开发服务器监听的地址。默认情况下,它在 localhost 上运行, 改为 flask run --host=0.0.0.0 (或 app.run(host="0.0.0.0") )在您机器的所有 IP 地址上运行。0.0.0.0是一个特殊值,您不能直接在浏览器中使用,您需要导航到网络上机器的实际 IP 地址。您可能还需要调整防火墙以允许外部访问端口。
flask quickstart docs在“外部可见服务器”部分对此进行解释:

If you run the server you will notice that the server is onlyaccessible from your own computer, not from any other in the network.This is the default because in debugging mode a user of theapplication can execute arbitrary Python code on your computer.

If you have the debugger disabled or trust the users on your network,you can make the server publicly available simply by adding--host=0.0.0.0 to the command line:

$ flask run --host=0.0.0.0

This tells your operating system to listen on all public IPs.

关于python - 将 Flask 开发服务器配置为在整个网络中可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40608037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com