本文介绍了Cherrypy服务的路径为404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于三个简单的应用程序:让我们使用不同于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 28130tcp4 0 0 127.0.0.1.28130 *.*听 

应用程序日志记录同意:

  CherryPy Checker:挂载在'/fusion'上的应用程序具有以其脚本名称开头的配置条目:'/fusion'CherryPy Checker:挂载在'/mobile_to_fusion'上的应用程序具有以其脚本名称开头的配置条目:'/mobile_to_fusion'CherryPy Checker:挂载在'/admin'上的应用程序具有以其脚本名称开头的配置条目:'/admin' 

但是访问网址时:

为什么 Cherrypy 找不到路径?

解决方案

AdminListener 类安装在/admin 下,而 AdminListener 不安装具有 default index 方法,以便能够在/admin 下挂载 AdminListener 的实例,并期望它能够工作.例如,在您当前的实现中,/admin/admin 应该可以工作.

您可以:

    AdminListener 中定义
  1. def default(self)或 def index(self)(替换 admin 方法)或
  2. ''下安装 AdminListener 的实例.像 cherrypy.tree.mount(adminListener,",adminConf)一样,这具有将 adminConf 应用于所有子应用程序的副作用,所以我想正确的方法是选项 1 .

例如,对于选项1:

 类AdminListener(object):@ cherrypy.exposedef索引(self,param = None):返回来自管理员的问候" 

或者

 类AdminListener(object):@ cherrypy.exposedef默认值(self,param = None):返回来自管理员的问候" 

主要区别在于 index 方法不使用位置参数之类的url片段,例如/admin/one 无效,但/admin/?param = one 将与 index default 一起使用(注意第二个/很重要).

default 方法类似于 catch-all 替代方法,将为应用程序挂载点下的所有未定义路径调用该方法.

For three simple apps: let us use a different port than 8080:

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

Let us set up three apps:

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

We can see it running on the correct port:

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

The application logging agrees:

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'

But when accessing the url: http://localhost:28130/admin - It is not found?

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

Why is Cherrypy not finding the paths?

解决方案

The AdminListener class is mounted under /admin and AdminListener does not have a default or index method to be able to mount an instance of AdminListener under /admin and expect it to work. For instance, with your current implementation/admin/admin should work.

You can either:

  1. Define def default(self) or def index(self) in AdminListener (replace the admin method) or
  2. Mount an instance of AdminListener under ''. Like cherrypy.tree.mount(adminListener, "",adminConf), this will have the side effect of having the adminConf apply to all the sub-application, so I suppose the right approach would be option 1.

For example, for option 1:

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

alternatively

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

The main difference is that the index method does not consume url fragments like positional parameters, for example /admin/one would not work, but /admin/?param=one will work with index and default (notice the second /, is important).

The default method is like a catch-all alternative, it will be called for any undefined path under the mountpoint of the app.

这篇关于Cherrypy服务的路径为404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:00