【问题标题】:404 for path served by Cherrypy404 为 Cherrypy 服务的路径
【发布时间】:2020-03-25 01:33:37
【问题描述】:

对于三个简单的应用程序:让我们使用与 8080 不同的端口:

cherrypy.config.update({'server.socket_host': '127.0.0.1',
                    'server.socket_port': 28130
                   })

让我们设置三个应用程序:

fusionConf = { '/fusion':{}} mobileConf = { r"/mobile_to_fusion":{}} adminConf = { '/admin':{}}

cherrypy.tree.mount(fusionListener, r"/fusion",fusionConf) cherrypy.tree.mount(mobileListener, r"/mobile_to_fusion",mobileConf) cherrypy.tree.mount(adminListener, r"/admin",adminConf) #

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

我们可以看到它在正确的端口上运行:

$netstat -an | grep 28130
tcp4       0      0  127.0.0.1.28130        *.*                    LISTEN

应用记录同意:

CherryPy Checker:
The application mounted at '/fusion' has config entries that start with its script name: '/fusion'

CherryPy Checker:
The application mounted at '/mobile_to_fusion' has config entries that start with its script name: '/mobile_to_fusion'

CherryPy Checker:
The application mounted at '/admin' has config entries that start with its script name: '/admin'

但是访问url时:http://localhost:28130/admin - 没找到?

404 Not Found
The path '/admin' was not found.

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/cherrypy/_cprequest.py", line 638, in respond
    self._do_respond(path_info)
  File "/usr/local/lib/python3.8/site-packages/cherrypy/_cprequest.py", line 697, in _do_respond
    response.body = self.handler()
  File "/usr/local/lib/python3.8/site-packages/cherrypy/lib/encoding.py", line 219, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/cherrypy/_cperror.py", line 416, in __call__
    raise self
cherrypy._cperror.NotFound: (404, "The path '/admin' was not found.")
Powered by CherryPy 18.5.0

为什么Cherrypy 找不到路径?

【问题讨论】:

    标签: python cherrypy


    【解决方案1】:

    AdminListener 类挂载在/admin 下,而AdminListener 没有defaultindex 方法能够在AdminListener 下挂载AdminListener 的实例并期望它能够工作.例如,您当前的实现/admin/admin 应该可以工作。

    您可以:

    1. AdminListener 中定义def default(self)def index(self)(替换admin 方法)或
    2. '' 下挂载AdminListener 的实例。与cherrypy.tree.mount(adminListener, "",adminConf) 一样,这将产生将adminConf 应用于所有子应用程序的副作用,因此我认为正确的方法是选项1

    例如,对于选项 1:

      class AdminListener(object):
          @cherrypy.expose
          def index(self, param=None):
              return "Hello from Admin"
    

    或者

      class AdminListener(object):
          @cherrypy.expose
          def default(self, param=None):
              return "Hello from Admin"
    

    主要区别在于index方法不会像位置参数那样消耗url片段,例如/admin/one不会工作,但/admin/?param=one会和indexdefault一起工作(注意第二个@ 987654347@,很重要)。

    default 方法就像一个 catch-all 替代方法,它将为应用程序挂载点下的任何未定义路径调用。

    【讨论】:

    • 谢谢!我没有发现index()必需的 及其作用的文档。我查看了整个文档和一堆谷歌搜索结果页面。任何提示如何更好地使用这个网络框架 - 对于需要超越“Hello world”的人?
    • 要求index() 不是“常规”的东西:它在cherrypy 中是特定的(而且对我来说出乎意料)。有没有地方这么说?我的意思是你是怎么发现它是必需的?我确实仔细阅读了教程:那里也没有说明。
    • def default() 效果很好。我只是不知道如何阅读图书馆开发人员的想法。实际上有很多数量的文档。但那为什么不提这个呢。
    • CherryPy 大约 18 岁;我相信在几年前的文档重构过程中,一些信息“丢失”了。它仍然在 Python 模块的文档字符串中,但并不是所有的都被渲染成文档......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多