HansDampf
HansDampf

Reputation: 11

Cherrypy is reachable but not showing anything

I wrote a little cherrypy "HelloWorld" example and the cp starts without problem. But I only see an empty page when I do a request to http://domain.com:8888

If I change the port of the request, I get an error of the browser that this resource is not reachable, so I guess the cp is generally reachable but not showing anything.

Any ideas what I am doing wrong ?

Here is the source of the 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()

Upvotes: 1

Views: 121

Answers (1)

saaj
saaj

Reputation: 25224

Where did you get cp.root = HelloWorld() from? There's no expectation on CherryPy side for value of the attribute so it doesn't make more sense than cp.blahblah = HelloWorld(). Yourrun_server should look like:

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()

Also your default handler doesn't seem to be correct either. It needs at least a variable positional arguments parameter, e.g. *args. CherryPy will fill it with path segments, e.g. ('foo', 'bar') for /foo/bar.

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

Upvotes: 1

Related Questions