我不确定这是否是特定的Flask,但当我在dev模式下运行应用程序(http://localhost:5000)时,我无法从网络上的其他机器访问它(使用http://[dev-host-ip]:5000)。例如,当Rails处于开发模式时,它工作得很好。我找不到任何关于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/
其他回答
如果以上解决方案都不起作用,请尝试手动在url的开头添加“http://”。
Chrome可以从搜索查询中区分“[ip-address]:5000”。但有时候,这种方式会起作用一段时间,然后就停止了联系,似乎我什么都没改变。我的假设是浏览器有时可能会自动在https://前加上前缀(它不应该这样,但这在我的例子中解决了这个问题)。
添加主机='0.0.0.0'到app.run '。
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
如果你得到OSError: [WinError 10013]试图访问一个套接字在Windows上的访问权限禁止的方式,你要么没有权限使用端口,或者其他东西正在使用它,你可以通过netstat -na|findstr 5000找到。
对我来说,我遵循上面的答案,并稍加修改:
只需在命令提示符上使用ipconfig获取ipv4地址 转到flask代码所在的文件 在主函数中写入app.run(host= '你的ipv4地址')
Eg:
如果您使用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)
)
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 在Python中重置生成器对象