我写了一个小樱桃“HelloWorld”示例,cp
启动没有问题。但当我向http://domain.com:8888发出请求时,我只看到一个空页面。
如果我更改请求的端口,我会收到浏览器的错误消息,指出此资源不可访问,因此我猜测 cp
通常可以访问,但不会显示任何内容。
知道我做错了什么吗?
这是cp的来源:
import MySQLdb as mdb
import cherrypy as cp
class HelloWorld(object):
@cp.expose
def index(self):
return ("gurk")
@cp.expose
def default(self):
return "default"
def run_server():
# Set the configuration of the web server
cp.config.update({
'engine.autoreload.on': True,
'log.screen': True,
'server.socket_port': 8888,
'server.socket_host': '0.0.0.0'
})
# Start the CherryPy WSGI web server
cp.root = HelloWorld()
cp.engine.start()
cp.engine.block()
if __name__ == "__main__":
cp.log("main")
run_server()
最佳答案
你从哪里获得cp.root = HelloWorld()
? CherryPy 方面对该属性的值没有任何期望,因此它并不比 cp.blahblah = HelloWorld() 更有意义。您的run_server
应如下所示:
def run_server():
# Set the configuration of the web server
cp.config.update({
'engine.autoreload.on': True,
'log.screen': True,
'server.socket_port': 8888,
'server.socket_host': '0.0.0.0'
})
# Mount the application to CherryPy tree
cp.tree.mount(HelloWorld(), '/')
# Start the CherryPy WSGI web server
cp.engine.start()
cp.engine.block()
此外,您的默认
处理程序似乎也不正确。它至少需要一个可变的位置参数参数,例如*args
。 CherryPy 将用路径段填充它,例如('foo', 'bar')
表示 /foo/bar
。
@cp.expose
def default(self, *args):
return "default {0}".format(','.join(args))
关于python - Cherrypy 可以访问,但没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26226483/