在CherryPy中找不到路径 [英] Path Not Found in CherryPy
问题描述
我一直在努力在CherryPy中实现测试API.
I've been working on implementing a test API in CherryPy.
我已经阅读了一些论坛,教程,并将旧的Python开发人员在工作中编写的代码拼凑在一起,这就是我所得到的:
I've read a few forums, tutorials and pieced together the code that the old Python developer at work had written and this is what I've got:
import json
import cherrypy
class person:
def default(self, *args):
r = {
'firstName': args[0],
'lastName': args[1],
'age': args[2]
}
return json.dumps(r)
default.exposed = True
class api:
def __init__(self):
self.person = person()
class Root:
def __init__(self):
self.api = api()
conf = {
'/': { 'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
}
cherrypy.config.update(conf)
cherrypy.tree.mount(Root())
cherrypy.quickstart()
运行此代码的结果是以下错误:
The result of running this code is the following error:
找不到404
找不到路径'/api/person/Blake/Williams/27'.
The path '/api/person/Blake/Williams/27' was not found.
回溯(最近通话最近):响应文件"/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cprequest.py",第656行response.body = self.handler()调用中的文件"/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/lib/encoding.py",第188行self.body = self.oldhandler(* args,** kwargs)调用中的文件"/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cperror.py",行386提高自我
Traceback (most recent call last): File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond response.body = self.handler() File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/lib/encoding.py", line 188, in call self.body = self.oldhandler(*args, **kwargs) File "/Users/blakewilliams/Programming/Practice/Python/VirtualEnv/foo/lib/python2.7/site-packages/cherrypy/_cperror.py", line 386, in call raise self
未找到:(404,未找到路径'/api/person/Blake/Williams/27'.")
NotFound: (404, "The path '/api/person/Blake/Williams/27' was not found.")
我不确定自己做错了什么...在此先感谢您的帮助.
I'm not sure what I'm doing wrong... Thanks in advance for any help.
推荐答案
如果像这样启动您的cherrypy服务器,那么它将起作用:
If you start your cherrypy server like this, then it works:
cherrypy.quickstart(cherrypy.Application(Root()), '/', {})
!警告:仍然找不到您指定的路径:您忘记了 api
级别:
! Caution: The path you indicate is still not found: you forget the api
level:
http://127.0.0.1:8080/api/person/Blake/Williams/27
这篇关于在CherryPy中找不到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!