python - Cherrypy 可以访问,但没有显示任何内容

标签 python cherrypy

我写了一个小樱桃“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/

相关文章:

python - 如何将cherrypy.session()存储在变量中?

python - CherryPy 缓存如何工作?

python - 用列表值反转字典

json - 如何在 CherryPy 的 POST 请求中接收 JSON?

python - rqworker超时

python - Tkinter 时钟因无限循环而崩溃?

python - 如何让 Cherry Py 将浏览器重定向到网站?

cherrypy - 是否可以暂停cherrypy服务器以便在不停止的情况下更新静态文件/数据库?

python - 如何装饰父类并让子类使用它? Python

python - 如何从 url 中删除查询字符串?