python flask ...我的电脑无法显示

时间:2015-03-01 22:18:52

标签: python flask

这是来自烧瓶主页的代码。

 from flask import Flask
 app = Flask(__name__)

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

if __name__ == '__main__':
    app.run(host='0.0.0.0')
    app.run(debug=True)

当我运行代码然后输入http://0.0.0.0:5000/ 我的电脑无法显示...... 问题是什么?

1 个答案:

答案 0 :(得分:0)

1)这不是烧瓶主页上的代码。

2)两次调用run()将无效。如果第一个run()将完成执行并转到第二个run(),第二个run(不会完成执行并到达程序结束并终止吗?

app.run()是这样的defined

run(host=None, port=None, debug=None, **options)

Changed in version 0.10: The default port is now picked from the SERVER_NAME variable.

Parameters: 
host – the hostname to listen on. Defaults to '127.0.0.1'. 
       Set this to '0.0.0.0' to have the server available externally as well. 

port – the port of the webserver. Defaults to 5000 or the port defined in the SERVER_NAME config variable if present.
debug – if given, enable or disable debug mode. See debug.
options – the options to be forwarded to the underlying Werkzeug server. See werkzeug.serving.run_simple() for more information.

您可以在一次app.run()来电中设置所有这些参数。

3)来自flask docs

  

如果您运行服务器[使用app.run()],您会注意到   服务器只能从您自己的计算机访问,而不能从任何其他计算机访   在网络中。这是默认值,因为在调试模式下是用户   应用程序可以在您的计算机上执行任意Python代码。

     

如果禁用了调试或信任网络上的用户,则可以   只需更改呼叫即可使服务器公开   run()方法看起来像这样:

app.run(host='0.0.0.0')
     

这会告诉您的操作系统监听所有公共IP。

如果您尝试将主机设置为0.0.0.0,然后启动服务器,则会显示消息:

  

但是,这并不意味着0.0.0.0是主机名(= IP地址)。 0.0.0.0表示服务器正在侦听端口5000上的任何公共主机名(= IP地址)。网址http://localhost:5000也将起作用。

底线:除非您知道自己在做什么,否则不要使用主机0.0.0.0。