使用Google Colab上的Python Flask连接到本地主机吗? [英] Connecting to localhost using Python-flask on Google Colab?

查看:48
本文介绍了使用Google Colab上的Python Flask连接到本地主机吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是烧瓶的新来者.我无法访问localhost:5000或127.0.0.1:5000.我正在用烧瓶.我尝试了许多解决方案,但没有一个对我有用.这是代码

I am a newcomer to flask. I cannot access localhost:5000 or 127.0.0.1:5000 . I am using flask . I have tried many solutions but none of them worked for me. Here's the code

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return ('Hello World')

if __name__ == '__main__':
   app.run()

我明白了

 * Serving Flask app "__main__" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

当我进入localhost:5000或127.0.0.1:5000时,我会得到

when i go to localhost:5000 or 127.0.0.1:5000 , i get

127.0.0.1拒绝连接**或**本地主机拒绝连接

127.0.0.1 refused to connect** or **localhost refused to connect

推荐答案

尝试像这样运行您的应用程序:

Try to run your app like this:

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000)

这也使服务器在外部可见.如果计算机的IP地址是 192.168.X.X ,则可以从同一网络通过5000端口访问它.

This also makes the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port.

防火墙也可能是一个问题,在这种情况下,请执行以下操作:

It could also be an issue with the firewall, in which case, do the following:

sudo ufw allow 5000

由于您正在Google Colab上运行,而不是在本地系统上运行.运行步骤会有所不同.

Since you are running on Google Colab and not on your local system. The running steps would vary.

请注意

Google Colab提供了一个VM(虚拟机),因此我们无法像运行本地Web服务器时那样在本地计算机上访问localhost(它会将其路由到本地计算机的本地主机).我们可以做的是使用ngrok将其公开到公共URL.这是Python库flask-ngrok.

Google Colab provides a VM(virtual machine) so we cannot access the localhost(all it does it route it to our local machine’s localhost) as we do on our local machine when running a local web server. What we can do is expose it to a public URL using ngrok. Here comes the Python library flask-ngrok.

按照步骤此处使其与 flask-ngrok

这篇关于使用Google Colab上的Python Flask连接到本地主机吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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