2014-10-06 83 views
1

我写了一点cherrypy“HelloWorld”示例,并且cp开始没有问题。但是我只看到一个空白页时,我做的http://domain.com:8888Cherrypy可达,但没有显示任何东西

的请求。如果我改变请求的端口,我得到这个资源是不可达的浏览器的错误,所以我想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() 

回答

1

你从哪里得到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() 

而且你default处理程序似乎不正确或者。它至少需要一个变量位置参数参数,例如*args。 CherryPy将填充路径段,例如('foo', 'bar')/foo/bar

@cp.expose 
def default(self, *args): 
    return "default {0}".format(','.join(args)) 
相关问题