我不确定这是否是特定的Flask,但当我在dev模式下运行应用程序(http://localhost:5000)时,我无法从网络上的其他机器访问它(使用http://[dev-host-ip]:5000)。例如,当Rails处于开发模式时,它工作得很好。我找不到任何关于Flask开发服务器配置的文档。你知道应该配置什么来实现这个功能吗?
当前回答
我也有同样的问题,我使用PyCharm作为编辑器,当我创建项目时,PyCharm创建了一个Flask服务器。我所做的就是用Python创建一个服务器,方法如下:
基本上我所做的就是创建一个新的服务器,如果不是python的话,就是flask
我希望这对你有所帮助
其他回答
如果您使用flask可执行文件启动服务器,请使用flask run——host=0.0.0.0将默认值从127.0.0.1更改为开放给非本地连接。
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.
参考:https://flask.palletsprojects.com/quickstart/
如果你的酷应用程序从外部文件加载了它的配置,就像下面的例子,那么不要忘记用HOST="0.0.0.0"更新相应的配置文件
cool.app.run(
host=cool.app.config.get("HOST", "localhost"),
port=cool.app.config.get("PORT", 9000)
)
我也有同样的问题,我使用PyCharm作为编辑器,当我创建项目时,PyCharm创建了一个Flask服务器。我所做的就是用Python创建一个服务器,方法如下:
基本上我所做的就是创建一个新的服务器,如果不是python的话,就是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.
您还可以通过环境变量设置主机(将其暴露在面向IP地址的网络上)和端口。
$ export FLASK_APP=app.py
$ export FLASK_ENV=development
$ export FLASK_RUN_PORT=8000
$ export FLASK_RUN_HOST=0.0.0.0
$ flask run
* Serving Flask app "app.py" (lazy loading)
* Environment: development
* Debug mode: on
* Running on https://0.0.0.0:8000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 329-665-000
参见如何获取所有可用的命令选项来设置环境变量?
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用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中获得所有直接子目录