Connecting to localhost using Python-flask on Google Colab?

[亡魂溺海] 提交于 2020-08-26 07:54:28

问题


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()

I get this

 * 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)

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

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


回答1:


Try to run your app like this:

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=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

Edit:

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

Do note that

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.

Follow the steps here to get it running with flask-ngrok



来源:https://stackoverflow.com/questions/60684068/connecting-to-localhost-using-python-flask-on-google-colab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!