运行cherrypy的hello world examp的问题

2024-06-16 09:48:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用他们网站上的例子来测试cherrypy框架:

import cherrypy
class HelloWorld(object):
    def index(self):
    return "Hello World!"
    index.exposed = True

cherrypy.quickstart(HelloWorld())

当我运行它时,我在控制台中得到这个响应:

[05/Dec/2011:00:15:11] ENGINE Listening for SIGHUP.
[05/Dec/2011:00:15:11] ENGINE Listening for SIGTERM.
[05/Dec/2011:00:15:11] ENGINE Listening for SIGUSR1.
[05/Dec/2011:00:15:11] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.

[05/Dec/2011:00:15:11] ENGINE Started monitor thread '_TimeoutMonitor'.
[05/Dec/2011:00:15:11] ENGINE Started monitor thread 'Autoreloader'.
[05/Dec/2011:00:15:12] ENGINE Serving on 127.0.0.1:8080
[05/Dec/2011:00:15:12] ENGINE Bus STARTED

当在本地运行浏览器并指向localhost:8080时,它可以工作,但是当使用server ip:8080时,它不能工作。我需要在某个地方设置服务器的ip地址吗?


Tags: ip框架forindex网站threadenginedec
1条回答
网友
1楼 · 发布于 2024-06-16 09:48:22

默认情况下cherrypy.quickstart只绑定到本地主机127.0.0.1,本地主机可以从服务计算机访问,但不能从通过网络连接到它的计算机访问。
如果希望能够从另一台计算机访问站点,则需要设置配置,如here中所述。

下面是一个基本的例子,只是将cherrypy更改为绑定到所有网络接口。

import cherrypy
class HelloWorld(object):
    def index(self):
        return "Hello World!"
    index.exposed = True

# bind to all IPv4 interfaces
cherrypy.config.update({'server.socket_host': '0.0.0.0'})
cherrypy.quickstart(HelloWorld())

相关问题 更多 >