Cherrypy返回NotFound:(404,“未找到路径'/'。”)

时间:2012-04-02 21:52:00

标签: python cherrypy

我是新手,我正在尝试安装一个简单的hello world应用程序,但它一直返回“NotFound:(404,”找不到路径'/'。“)”,但我已经定义了它。

这是我得到的,

__ init __ .py

import cherrypy
from HomeNetMain import HomeNetMain

cherrypy.config.update("global.cfg")
#I have tried "" for the script name parm but that doesn't work
cherrypy.tree.mount(HomeNetMain,"/","main.cfg") 
cherrypy.engine.start()
cherrypy.quickstart()

我有另一个文件

import cherrypy

class HomeNetMain:

    @cherrypy.expose
    def index(self):
       return "Hello World"

我试过装饰器和index.exposed = True都没有用(子问题是装饰器或index.exposed的首选方法是什么)

global.cfg

[global]
server.socket_host: "127.0.0.1"
server.socket_port: 9080
log.screen: True
log.error_file: "/tmp/cherrypy.error"
log.access_file: "/tmp/cherrypy.access"

main.cfg

[/]
log.screen: True
log.error_file: "/tmp/homenet.error"
log.access_file: "/tmp/homenet.access"

我感谢任何帮助,提前谢谢。

修改

这是完整的堆栈跟踪

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.2-py2.7.egg/cherrypy/_cperror.py", line 386, in __call__
raise self
NotFound: (404, "The path '/' was not found.")

2 个答案:

答案 0 :(得分:4)

原来我不应该使用cherrypy.quickstart()我将代码更改为以下内容并且工作正常

cherrypy.engine.start()
cherrypy.engine.block()

答案 1 :(得分:0)

正在安装 HomeNetMain,而不是实例。由于 index 不是classmethod,因此将抛出TypeError,框架将其作为404处理。

cherrypy.tree.mount(HomeNetMain(), "/", "main.cfg")