运行 cherrypy 的 hello world 示例时遇到的问题
我正在尝试通过他们网站上的示例来测试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时可以正常工作,但当我用服务器的ip地址访问serverip:8080时就不行了。我需要在某个地方设置服务器的ip地址吗?
1 个回答
13
默认情况下,cherrypy.quickstart
只会绑定到本地地址 127.0.0.1
,这意味着只有在运行这个程序的电脑上才能访问,而其他通过网络连接的电脑是无法访问的。
如果你想让其他电脑也能访问这个网站,你需要按照文档中的说明来设置配置,具体可以查看 这里。
下面是一个简单的例子,演示如何将 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())