我不确定这是否是特定的Flask,但当我在dev模式下运行应用程序(http://localhost:5000)时,我无法从网络上的其他机器访问它(使用http://[dev-host-ip]:5000)。例如,当Rails处于开发模式时,它工作得很好。我找不到任何关于Flask开发服务器配置的文档。你知道应该配置什么来实现这个功能吗?
当前回答
虽然这是可能的,但您不应该在生产中使用Flask开发服务器。Flask开发服务器的设计并不是特别安全、稳定或高效。有关正确的解决方案,请参阅部署文档。
flask run的——host选项或app.run()的host参数控制开发服务器侦听的地址。默认情况下,它运行在localhost上,将其更改为flask run——host=0.0.0.0(或app.run(host="0.0.0.0"))以运行在您机器的所有IP地址上。
0.0.0.0是一个不能直接在浏览器中使用的特殊值,您需要在网络中导航到机器的实际IP地址。您可能还需要调整防火墙以允许外部访问该端口。
Flask快速入门文档在“外部可见服务器”部分解释了这一点:
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. 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.
其他回答
虽然这是可能的,但您不应该在生产中使用Flask开发服务器。Flask开发服务器的设计并不是特别安全、稳定或高效。有关正确的解决方案,请参阅部署文档。
flask run的——host选项或app.run()的host参数控制开发服务器侦听的地址。默认情况下,它运行在localhost上,将其更改为flask run——host=0.0.0.0(或app.run(host="0.0.0.0"))以运行在您机器的所有IP地址上。
0.0.0.0是一个不能直接在浏览器中使用的特殊值,您需要在网络中导航到机器的实际IP地址。您可能还需要调整防火墙以允许外部访问该端口。
Flask快速入门文档在“外部可见服务器”部分解释了这一点:
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. 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.
在项目根目录中创建文件.flaskenv。
这个文件中的参数通常是:
FLASK_APP=app.py
FLASK_ENV=development
FLASK_RUN_HOST=[dev-host-ip]
FLASK_RUN_PORT=5000
如果您有一个虚拟环境,请激活它并执行pip install python-dotenv。
这个包将使用.flaskenv文件,其中的声明将自动跨终端会话导入。
然后你就可以进行长距离跑步了
我也有同样的问题,我使用PyCharm作为编辑器,当我创建项目时,PyCharm创建了一个Flask服务器。我所做的就是用Python创建一个服务器,方法如下:
基本上我所做的就是创建一个新的服务器,如果不是python的话,就是flask
我希望这对你有所帮助
这最终对我有用。
import os
然后把它放在你的python app.py或主文件的末尾。
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
这个答案并不仅仅与flask有关,而是应该适用于所有无法从其他主机连接服务的问题。
使用netstat -ano | grep <port>查看地址是否为0.0.0.0或::。如果它是127.0.0.1,那么它仅用于本地请求。 使用tcpdump查看是否有数据包丢失。如果有明显的不平衡,请通过iptables检查路由规则。
今天我像往常一样运行我的flask应用程序,但我注意到它不能从其他服务器连接。然后我运行netstat -ano | grep <端口>,本地地址是::或0.0.0.0(我都尝试了,我知道127.0.0.1只允许从本地主机连接)。然后我使用telnet主机端口,结果就像连接到....这很奇怪。然后我想我最好用tcpdump -i any port <port> -w w.pcap检查它。我注意到它是这样的:
然后通过检查iptables——list OUTPUT部分,我可以看到几个规则:
这些规则禁止在握手时输出TCP重要数据包。删除它们,问题就解决了。
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 使用try和。Python中的if
- 如何在Python中获得所有直接子目录