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

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

问题描述

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

解决方案

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






为您的 app.run()添加一个参数。默认情况下,它在本地主机上运行,​​将其更改为 app.run(host ='0.0.0.0')以在您的机器IP地址上运行。



快速入门页面


外部可见服务器

如果你运行服务器,你会注意到服务器只能从你自己的计算机上获得
,而不能从网络上的任何其他服务器上获得。
这是缺省值,因为在调试模式下,
应用程序的用户可以在您的计算机上执行任意Python代码。如果您的
禁用了调试或者信任网络上的用户,您可以使
服务器公开可用。



只需更改 run()方法如下所示:

app.run(host = 0.0.0.0')



这会告诉您的操作系统在公共IP上进行监听。



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?

解决方案

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.


Add a parameter to your app.run(). By default it runs on localhost, change it to app.run(host= '0.0.0.0') to run on your machines IP address.

Documented on the Flask site under "Externally Visible Server" on the Quickstart page:

Externally Visible Server

If you run the server you will notice that the server is only available 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. If you have debug disabled or trust the users on your network, you can make the server publicly available.

Just change the call of the run() method to look like this:

app.run(host='0.0.0.0')

This tells your operating system to listen on a public IP.

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

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