将 Flask 开发服务器配置为在整个网络中可见 [英] Configure Flask dev server to be visible across the network

查看:38
本文介绍了将 Flask 开发服务器配置为在整个网络中可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm not sure if this is Flask specific, but when I run an app in dev mode (http://localhost:5000), I cannot access it from other machines on the network (with http://[dev-host-ip]:5000). With Rails in dev mode, for example, it works fine. I couldn't find any docs regarding the Flask dev server configuration. Any idea what should be configured to enable this?

推荐答案

虽然这是可能的,但您不应在生产中使用 Flask 开发服务器.Flask 开发服务器的设计并不是特别安全、稳定或高效.请参阅关于部署的文档以获取正确的解决方案.

While this is possible, you should not use the Flask dev server in production. The Flask dev server is not designed to be particularly secure, stable, or efficient. See the docs on deploying for correct solutions.

flask run--host 选项,或 app.run()host 参数, 控制开发服务器侦听的地址.默认情况下它在 localhost 上运行,将其更改为 flask run --host=0.0.0.0 (或 app.run(host=0.0.0.0";)) 在您机器的所有 IP 地址上运行.

The --host option to flask run, or the host parameter to app.run(), controls what address the development server listens to. By default it runs on localhost, change it to flask run --host=0.0.0.0 (or app.run(host="0.0.0.0")) to run on all your machine's IP addresses.

0.0.0.0 是一个特殊的值,你不能直接在浏览器中使用,你需要导航到网络上机器的实际 IP 地址.您可能还需要调整防火墙以允许外部访问该端口.

0.0.0.0 is a special value that you can't use in the browser directly, you'll need to navigate to the actual IP address of the machine on the network. You may also need to adjust your firewall to allow external access to the port.

Flask 快速入门文档在外部可见服务器"中对此进行了解释;部分:

The Flask quickstart docs explain this in the "Externally Visible Server" section:

如果你运行服务器,你会注意到服务器只是可以从您自己的计算机访问,而不是从网络中的任何其他计算机访问.这是默认设置,因为在调试模式下,应用程序可以在您的计算机上执行任意 Python 代码.

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

如果您禁用了调试器或信任您网络上的用户,您只需添加即可使服务器公开可用--host=0.0.0.0 到命令行:

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

这会告诉您的操作系统侦听所有公共 IP.

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

这篇关于将 Flask 开发服务器配置为在整个网络中可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆